Version: 1.3
Type: Function
Category: HTML
License: GNU General Public License
Description: Takes an array and converts it into an html pop-up box
// associative array function build_select_box_from_array ($vals, $select_name, $checked_val = FALSE) { /* Takes one array, with the array keys being the displayed name and the array values as the values. The second parameter is the name you want assigned to this form element. The third parameter is optional. Pass the value of the item that should be checked. */ $str = sprintf('<SELECT name="%s">', $select_name); $optionStr = '<OPTION value="%s"%s>%s</OPTION>'; $selected = " selected"; $notSelected = ""; foreach ($vals as $key => $value) { if($value == $checked_val) { $sel = $selected; } else { $sel = $notSelected; } $str .= sprintf($optionStr, $value, $sel, $key); } $str .= "</SELECT>"; return $str; } // two arrays function build_select_box_from_arrays ($names, $values, $select_name, $checked_val = FALSE) { /* Takes two arrays, the first being the displayed name and the second the true value of the option. The third parameter is the name you want assigned to this form element. The fourth parameter is optional. Pass the value of the item that should be checked. */ /* replace the foreach with this */ foreach ($values as $key => $value) { if($value == $checked_val) { $sel = $selected; } else { $sel = $notSelected; } $str .= sprintf($optionStr, $value, $sel, $names[$key]); } } /* example usage - create a select box with 4 alternatives each corresponding to a number */ /* associative array */ $arr = array( "Slow" => 3, "Normal" => 7, "Fast" => 10, "FTL" => 100 ); $str = build_select_box_from_array($arr, "The speed of your current vehicle"); /* two arrays */ $names = array( "Slow", "Normal", "Fast", "FTL" ); $values = array( 3, 7, 10, 100 ); $str = build_select_box_from_arrays($names, $values, "The speed of your current vehicle");