|  | Posted by MS on 01/11/07 14:51 
> HI there.>
 > Ive got a quick question.
 >
 > How do I have a select box show a particular value.
 >
 > Same as a previous post but I dont think it got answered
 >
 
 I use the following function....
 
 // $name = the field name
 // $type = "selection" || "checkbox" || "radio", the type of field you want
 to display the data in.
 // $class = the class to use
 // $listarray = array("id","value")
 // $selected = Which item is selected within the list in the format
 "int,int,int" for radio and in the format "int" for selection & checkbox
 
 // So just by changing the $type in the calling function you can have the
 data displayed in 3 ways.
 
 function getFormInput($name,$type,$class,$listarray,$selected=""){
 $res = "";
 if($type == "selection"){
 $res .= "<select name='".$name."' class='".$class."'>\n";
 for ($i=0;$i <= count($listarray);$i++) {
 $sel = ($selected == $listarray[$i]['id']? " selected" : "");
 $res .= "<option
 value='".$listarray[$i]['id']."'$sel>".$listarray[$i]['name']."</option>\n";
 }
 $res .= "</select>\n";
 }elseif($type == "checkbox"){
 $selsplit = explode(',',$selected);
 for ($i=1;$i <= count($listarray);$i++) {
 $sel = (in_array($listarray[$i]['id'],$selsplit)? " checked" : "");
 $res .= "<input type='checkbox' name='".$name."[".$i."]'
 
 value='".$listarray[$i]['id']."'$sel>".$listarray[$i]['name']."<br>\n";
 }
 }elseif($type == "radio"){
 for ($i=1;$i <= count($listarray);$i++){
 $sel = ($listarray[$i]['id'] == $selected) ? " checked" : "";
 $res .= "<input type='radio' name='".$name."'
 
 value='".$listarray[$i]['id']."'$sel>".$listarray[$i]['name']."<br>\n";
 }
 }
 return $res;
 }
 
 
 Hope that helps
 
 MS
 Join MyClubWeb.co.uk - The Home of Club Websites and Medium to Small
 Businesses.
  Navigation: [Reply to this message] |