Jump to: navigation, search

PHP HTML Dynamic Drop Down List From Array

From w3cyberlearnings

Contents

PHP Dynamic Drop Down With Array

Make a HTML drop down list from a PHP array.

Example 1: A Normal Array

$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

<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

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

<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

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

Related Links


Dynamic HTML From Array
  1. Dynamic Drop Down List with Array
  2. Dynamic List with Array
  3. Dynamic Radio with Array
  4. Dynamic Checkbox with Array
Dynamic HTML From Database
  1. Dynamic Drop Down List with Database
  2. Dynamic List from Database
  3. Dynamic Radio from Database
  4. Dynamic Checkbox from database
Dynamic HTML with Ajax
  1. Dynamic Drop Down List with Ajax
  2. Dynamic List with Ajax
  3. Dynamic Radio with Ajax
  4. Dynamic Checkbox with Ajax
  5. Dynamic Form with Ajax
Others Related
  1. Dynamic Drop down with default option
  2. How to retrieve Form Data
Navigation
Web
SQL
MISC
References