PHP HTML Dynamic Drop Down List From Array

From CoderLearner
Jump to: navigation, search

PHP Dynamic Drop Down With Array

Make drop down list from PHP array.

Example 1: A Normal Array

  • In this example, it is an array that it is going to generate the HTML select elements.
$my_array = array("my name", "my age", "my grade");

echo '<select name="somethig">';
echo '<option value="">Select...</option>';
for ($i = 0; $i < count($my_array); $i++) {
	echo '<option value="' . $i . '">' ;
	echo $my_array[$i] ;
	echo '</option>';
}
echo '</select>';

Output: HTML Source

  • Here is the HTML select elements generated from the PHP script.
  • If you view it from the browser, you will be able to see a drop down list contains

the list of name.

<select name="somethig">
<option value="">Select...</option>
<option value="0">my name</option>
<option value="1">my age</option>
<option value="2">my grade</option>
</select>

Example 2:An Associative Array

  • OKay, it uses the associated array to generate the drop down list.
<?php

$my_array = array(
	 'GSU4300' => 'Lili Markina',
	 'GSU4301' => 'John Kokina',
	 'GSU4304' => 'Bill Clinong',
	 'GSU4305' => 'Obamark Chiko'
);

echo '<select name="somethig">';
echo '<option value="">Select...</option>';
foreach ($my_array as $k => $v) {
	echo '<option value="' . $k . '">' . $v . '</option>';
}
echo '</select>';
?>

Output

  • The select element option value is the associated array key.
<select name="somethig">
<option value="">Select...</option>
<option value="GSU4300">Lili Markina</option>
<option value="GSU4301">John Kokina</option>
<option value="GSU4304">Bill Clinong</option>
<option value="GSU4305">Obamark Chiko</option>
</select>

Example 3: Create PHP Custom Function for drop down List

  • Create a custom function so that you can reuse it.
  • The custom function accepts 4 parameters
  • First parameter is the array
  • Second parameter is the default select element option
  • Third parameter is the name of the select element <select name="">
  • Fourth parameter is to set the function return the value or just display the value.
<?php
function select($array, $select_value, $select_name="MY_SELECT_NAME", $return=0) {

	$select_str;
	$select_str = '<select name="' . $select_name . '">';
	$select_str.= '<option value="none">Select...</option>';
	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>';
		}
	}
	$select_str.= '</select>';
	if ($return) {
		return $select_str;
	} else {
		echo $select_str;
	}
}

$my_array = array('BMW', 'Ford', 'Honda', 'Toyota', 'Lexus', 'KIA', 'SCION');
$default_value = 'KIA';

select($my_array, $default_value, 'MY_CAR');
?>

Output:HTML Source

<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>