making <select>-tags

[php]function makeselect($name,$array)
{
echo ‘’;
foreach($array as $value => $displayed)
{
echo ‘<option value="’.htmlentities($value).’"’;
if(isset($_REQUEST[$name]) and $_REQUEST[$name]==$value)
echo ’ selected=“selected”’;
echo ‘>’.htmlentities($displayed).’’;
}
echo ‘’;
}[/php]

usage:

[code]<?php
$countries=array(
‘’=>‘Select Your Country’,
‘de’=>‘Germany’,
‘en’=>‘England’,
‘us’=>‘USA’
);
?>

... <?php makeselect("country",$countries) ?> ... [/code]

[php]
function makeselect ($name, $array, $default = “”)
{
$returnvalue = ‘’;
foreach($array as $value => $displayed)
{
$returnvalue .= ‘<option value="’.htmlentities($value).’"’;
if($default == $value) {
$returnvalue .= ’ selected=“selected”’;
}
$returnvalue .= ‘>’.htmlentities($displayed).’’;
}
$returnvalue .= ‘’;
return $returnvalue;
}
[/php]

For passing a default value and getting the output passed back, rather than echoed directly :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service