21st December 2007 - 2 minutes read time
Here is a function to generate a select box for a HTML form. The three parameters are:
- $name : The name of the select, this will appear in the "name" attribute.
- $options : An associative array containing all of the options.
- $default : The option that will be selected as default.
function generateSelect($name,$options,$default=''){
$html = '<select name="'.$name.'">';
foreach ($options as $value => $label) {
$html .= '<option value="' . $value . '">' . $label . '</option>';
}
$html .= '</select>';
return $html;
}
You can call the function like this:
echo generateSelect('selectPreference',array('yes'=>'yes','no'=>'no'),'yes');
Which produces the following HTML as output: