Php select function
From w3cyberlearnings
Contents |
PHP function select
This function makes HTML Select option from array.
Syntax select
- array: array input for select option
- select_value: default select value (for associative array, is the array key)
- select_name: Name the select option
- return (option): default value is 0, return value or not
- return=0: not return
- return=1: return
- option (optional):
- option=3: array key is the option value, and the array value is the label.
- option=2: array elements become value and label
- option=1: array element become the label, and the array index is the value.
select(array, select_value,select_name, return=0, option=1);
Function select()
<?php function select($array, $select_value, $select_name="MY_SELECT_NAME", $return=0, $option=1) { $select_str; $select_str = '<select name="' . $select_name . '">'; $select_str.= '<option value="none">Select...</option>'; if ($option == 1) { for ($i = 0; $i < count($array); $i++) { if ($array[$i] == $select_value) { $select_str.= '<option value="' . $i . '" selected="selected">'; $select_str.= $array[$i]; $select_str.= '</option>'; } else { $select_str.= '<option value="' . $i . '">'; $select_str.= $array[$i]; $select_str.= '</option>'; } } } else if ($option == 2) { for ($i = 0; $i < count($array); $i++) { if ($array[$i] == $select_value) { $select_str.= '<option value="' . $array[$i] . '" selected="selected">'; $select_str.= $array[$i]; $select_str.= '</option>'; } else { $select_str.= '<option value="' . $array[$i] . '">'; $select_str.= $array[$i]; $select_str.= '</option>'; } } } else if ($option == 3) { foreach ($array as $k => $v) { if ($k == $select_value) { $select_str.= '<option value="' . $k . '" selected="selected">'; $select_str.= $v; $select_str.= '</option>'; } else { $select_str.= '<option value="' . $k . '">'; $select_str.= $v; $select_str.= '</option>'; } } } $select_str.= '</select>'; if ($return) { return $select_str; } else { echo $select_str; } } ?>
Example 1:Array index is the select option values
<?php require_once 'select.php'; $my_array = array('BMW', 'Ford', 'Honda', 'Toyota', 'Lexus', 'KIA', 'SCION'); $default_value = 'KIA'; select($my_array, $default_value, 'MY_CAR'); ?>
Output
<select name="MY_CAR"> <option value="none">Select...</option> <option value="0">BMW</option> <option value="1">Ford</option> <option value="2">Honda</option> <option value="3">Toyota</option> <option value="4">Lexus</option> <option value="5" selected="selected">KIA</option> <option value="6">SCION</option> </select>
Example 2: Make array value is the select option values and label
<?php require_once 'select.php'; $my_array = array('BMW', 'Ford', 'Honda', 'Toyota', 'Lexus', 'KIA', 'SCION'); $default_value = 'KIA'; select($my_array, $default_value, 'ALL_CAR', 0, 2); ?>
Output
<select name="ALL_CAR"> <option value="none">Select...</option> <option value="BMW">BMW</option> <option value="Ford">Ford</option> <option value="Honda">Honda</option> <option value="Toyota">Toyota</option> <option value="Lexus">Lexus</option> <option value="KIA" selected="selected">KIA</option> <option value="SCION">SCION</option> </select>
Example 3:Array key is the select option value
<?php require_once 'select.php'; $my_array = array( 'a3' => 'A4 Serial 400', 'a4' => 'A4 Serial 453', 'bm31' => 'BMW Serial 31.4', 'bm23' => 'Super Mini'); select($my_array,'a4','CALL_CAR',0,3); ?>
Output
<select name="CALL_CAR"> <option value="none">Select...</option> <option value="a3">A4 Serial 400</option> <option value="a4" selected="selected">A4 Serial 453</option> <option value="bm31">BMW Serial 31.4</option> <option value="bm23">Super Mini</option> </select>