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
- Use mylist() 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
Dynamic HTML From Database
- Dynamic Drop Down List with Database
- Dynamic List from Database
- Dynamic Radio from Database
- Dynamic Checkbox from database
Dynamic HTML with Ajax
Others Related