Jump to: navigation, search

PHP HTML Dynamic List From Database

From w3cyberlearnings

Contents

PHP Dynamic Order LIST or un-order LIST From Database

Make HTML UL or OL list 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 '<ul>';
		while ($row = $result_db->fetch_object()) {
			echo '<li>';
			echo $row->name;
			echo '</li>';
		}
		echo '</ul>';
	}
}
$db->close();
?>

Output: HTML Source

<ul>
<li>Johny Zhar</li>
<li>Jacob Milli</li>
<li>lili lee</li>
<li>Michael Vieva</li>
<li>Emma lee</li>
<li>Peeou Zhou</li>
<li>Isabel Peom</li>
<li>Sophal Tang</li>
<li>Chong Mok</li>
<li>Ebo Kaun</li>
<li>salina lee</li>
<li>pheank so</li>
<li>lyekwan lee</li>
<li>kiloma otam</li>
</ul>

Example 2: Use select function

<?php
require_once 'mylist.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->name;
		}
		mylist($aa_student,2);
	}
}
$db->close();

Output: HTML Source

<ol>
<li>Johny Zhar</li>
<li>Jacob Milli</li>
<li>lili lee</li>
<li>Michael Vieva</li>
<li>Emma lee</li>
<li>Peeou Zhou</li>
<li>Isabel Peom</li>
<li>Sophal Tang</li>
<li>Chong Mok</li>
<li>Ebo Kaun</li>
<li>salina lee</li>
<li>pheank so</li>
<li>lyekwan lee</li>
<li>kiloma otam</li>
</ol>

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