render_select_option()

Description

Renders a select element.

Takes an array of options (as returned from sql_query and returns a valid
select element. The query must have a column aliased as value and label.
Option groups can be created as well with the optional $groupby parameter.
This function retrieves a language field in the form of
$lang['cfg-<fieldname>'] to use for the element label.

<code>
$options = sql_select("SELECT name AS label, ref AS value FROM resource_type");

render_select_option('myfield', $options, 18);
</code>

Parameters

ColumnTypeDefaultDescription
$fieldname string Name to use for the field.
$opt_array array Array of options to fill the select with
$selected mixed If matches value the option is marked as selected
$groupby string '' Column to group by

Return

string HTML output.

Location

include/config_functions.php lines 27 to 55

Definition

 
function render_select_option($fieldname$opt_array$selected$groupby '')
{
    global 
$errorfields$lang;

    
$output '';
    
$output .= "<tr><th><label for=\"$fieldname\">" $lang['cfg-' $fieldname] . "</label></th>";
    
$output .= "<td><select name=\"$fieldname\">";

    if (
$groupby != '') {
        
$cur_group $opt_array[0][$groupby];
        
$output .= "<optgroup label=\"$cur_group\">";
    }

    foreach (
$opt_array as $option) {
        if (
$groupby != '' && $cur_group != $option[$groupby]) {
            
$cur_group $option[$groupby];
            
$output .= "</optgroup><optgroup label=\"$cur_group\">";
        }
        
$output .= "<option ";
        
$output .= $option['value'] == $selected 'selected="selected" ' '';
        
$output .= "value=\"{$option['value']}\">{$option['label']}</option>";
    }

    
$output .= '</optgroup>';
    
$output .= isset($errorfields[$fieldname]) ? '<span class="error">* ' $errorfields[$fieldname] . '</span>' '';
    
$output .= '</td></tr>';

    return 
$output;
}

This article was last updated 23rd April 2025 21:35 Europe/London time based on the source file dated 17th April 2025 16:15 Europe/London time.