Jump to: navigation, search

PHP HTML Dynamic Drop Down List From Database

From w3cyberlearnings

Contents

PHP Dynamic Drop Down From Database

Make HTML Select option from database

Table: student

CREATE TABLE student
(
student_id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
PRIMARY KEY(student_id)
);

Records

NSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (1,'Johny','Zhar');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (2,'Jacob','Milli');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (3,'lili','lee');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (4,'Michael','Vieva');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (5,'Emma','lee');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (6,'Peeou','Zhou');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (7,'Isabel','Peom');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (8,'Sophal','Tang');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (9,'Chong','Mok');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (10,'Ebo','Kaun');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (11,'salina','lee');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (12,'pheank','so');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (13,'lyekwan','lee');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (14,'kiloma','otam');

Example 1

<?php

define('HOST', 'localhost');
define('USER', 'root');
define('PASS', 'yeething');
define('DBNAME', 'woowood');

$db = new mysqli(HOST, USER, PASS, DBNAME);

if ($db->connect_errno) {
	echo "Failed to connect to MySQL: (" . $db->connect_errno . ") "
	. $db->connect_error;
} else {
	$sql = "SELECT student_id, concat(first_name,' ',last_name) as name FROM student";
	$result_db = $db->query($sql);
	if (!$result_db) {
		echo $db->error . ' Error perform query!';
	} else {
		echo '<select name="student">';
		echo '<option value="">Select...</option>';
		while ($row = $result_db->fetch_object()) {
			echo '<option value="' . $row->student_id . '">';
			echo $row->name;
			echo '</option>';
		}
		echo '</select>';
	}
}
$db->close();
?>

Output: HTML Source

<select name="student">
<option value="">Select...</option>
<option value="1">Johny Zhar</option>
<option value="2">Jacob Milli</option>
<option value="3">lili lee</option>
<option value="4">Michael Vieva</option>
<option value="5">Emma lee</option>
<option value="6">Peeou Zhou</option>
<option value="7">Isabel Peom</option>
<option value="8">Sophal Tang</option>
<option value="9">Chong Mok</option>
<option value="10">Ebo Kaun</option>
<option value="11">salina lee</option>
<option value="12">pheank so</option>
<option value="13">lyekwan lee</option>
<option value="14">kiloma otam</option>
</select>

Example 2: Use select function

<?php

require_once 'select.php';

define('HOST', 'localhost');
define('USER', 'root');
define('PASS', 'yeething');
define('DBNAME', 'woowood');

$db = new mysqli(HOST, USER, PASS, DBNAME);

if ($db->connect_errno) {
	echo "Failed to connect to MySQL: (" . $db->connect_errno . ") "
	. $db->connect_error;
} else {
	$sql = "SELECT student_id, concat(first_name,' ',last_name) as name FROM student";
	$result_db = $db->query($sql);
	if (!$result_db) {
		echo $db->error . ' Error perform query!';
	} else {
		$aa_student = array();

		while ($row = $result_db->fetch_object()) {
			$aa_student[$row->student_id] = $row->name;
		}
		select($aa_student, $default_value = 3, 'sel_student', 0, 3);
	}
}
$db->close();
?>

Output: HTML Source

<select name="sel_student">
<option value="none">Select...</option>
<option value="1">Johny Zhar</option>
<option value="2">Jacob Milli</option>
<option value="3" selected="selected">lili lee</option>
<option value="4">Michael Vieva</option>
<option value="5">Emma lee</option>
<option value="6">Peeou Zhou</option>
<option value="7">Isabel Peom</option>
<option value="8">Sophal Tang</option>
<option value="9">Chong Mok</option>
<option value="10">Ebo Kaun</option>
<option value="11">salina lee</option>
<option value="12">pheank so</option>
<option value="13">lyekwan lee</option>
<option value="14">kiloma otam</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