Posts Tagged ‘xhtml’

HTML dropdown menu function

Tuesday, March 10th, 2009

If you have some sites that do not rely on a framework you could use this function to generate a simple HTML select.
Note: It won't preselect multiple values.
You could download the code because it usually doesn't look good in wordpress.

//echo getHTMLSelect('dropdown', array(0 => "", 1 => "'test'\"", 2 => "test2"), @$_REQUEST['sel']);

/**
* Returns an HTML dropdown menu.
*
* @param string $name HTML select name
* @param array $values options key value
* @param int $selected_id currently selected ID
* @param string $attr several attributes that will be added in <select ....>
* @return string
*/
function getHTMLSelect($name = 'dropdown', $values = array(), $selected_id = 0, $attr = '') {
$buff = "<select name=\"$name\" $attr>\n";
foreach($values as $k => $v) {
$s = ($selected_id == $k) ? " selected='selected'" : '';
$k = intval($k);
$v = htmlentities($v, ENT_QUOTES, 'UTF-8');
$buff .= "<option value=\"$k\" $s>$v</option>\n";
}

$buff .= "</select>\n";
return $buff;
}