Collections functions
Encryption functions
General functions
Render functions
Theme permission functions
Video functions
Resource functions



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.

$options = sql_select("SELECT name as label, ref as value FROM resource_types");

render_select_option('myfield', $options, 18);


$fieldname string Name to use for the field.
$opt_array string 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


string HTML output.


include/config_functions.php lines 31 to 53


function render_select_option($fieldname$opt_array$selected$groupby=''){
$output '';
$output .= "<tr><th><label for=\"$fieldname\">".$lang['cfg-'.$fieldname]."</label></th>";
$output .= "<td><select name=\"$fieldname\">";
    if (
$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>';

This article was last updated 1st March 2021 09:35 Europe/London time based on the source file dated 19th February 2021 16:00 Europe/London time.