Php Programming Code Examples
Php > HTML and Php Code Examples
Keeping selection from a drop down
Keeping selection from a drop down
<?
function my_select($name,$rows,$multiple,$array_list,$match)
{
//this select box takes the above 5 arguements to create details
/*
1. $name = the name of the select box
2. $rows = the number of displayed rows (1 as the default)
3. $multiple = does the select allow multiple choices (boolean value 0 for no, 1 for yes
4. $array_list = the options (in a value:display pair ie "1:Informatique","2:Voyages","3:Immobilier")
5. $match = the items to be selected if trying to keep a value ( is "" if no values present or comma separated list if multiple)
*/
$numOpts = count($match);
if ($numOpts>1){
$matches=implode(",",$match);
}elseif ($numOpts==1){
$matches=$match;
}else{
$matches="";
}
echo "<select name=\"$name\" ";
echo "size=\"$rows\" ";
if ($mutiple==1){ echo " MULTIPLE "; }
echo ">";
$count=count($array_list);
for ($i=0;$i<$count;$i++){
//take apart the array key:value pairs
$elements = explode(":",$array_list[$i]);
//compare matches to list to see if the item is selected
if (($numOpts<2)&&($matches == $elements[0])){
echo "<option value=\"".$elements[0]."\" SELECTED>".$elements[1];
}elseif(($numOpts>1)&&(in_array($elements[0],$matches))){
echo "<option value=\"".$elements[0]."\" SELECTED>".$elements[1];
}else{
echo "<option value=\"".$elements[0]."\">".$elements[1];
}
}//end for
echo "</select>";
}//end function
?>