ดึงข้อมูล จากฐานข้อมูล สร้าง รายการเครือข่าย แบบ tree ด้วย php
เขียนเมื่อ 12 ปีก่อน โดย Ninenik Narkdeephp tree ระบบเครื่อข่าย
คำสั่ง การ กำหนด รูปแบบ ตัวอย่าง เทคนิค ลูกเล่น การประยุกต์ การใช้งาน เกี่ยวกับ php tree ระบบเครื่อข่าย
ไปที่
Copy
ตัวอย่าง วิธีการเขียนโค้ด ต่อไปนี้ เป็นแนวทางสำหรับสร้าง รายการแบบ tree จากการดึงข้อมูลในฐานข้อมูล มาประยุกต์ใช้ ตัวอย่างผลลัพธ์
- 1
- 1.1
- 1.2
- 1.2.1
- 2
- 2.1
- 2.1.1
- 2.1.2
- 2.2
- 2.2.1
- 2.2.1.1
- 2.2.2
- 2.2.2.1
- 2.2.1
- 2.1
- 3
- 3.1
เราสามารถนำไปประยุกต์ เช่น สร้างเป็นรายการเมนู , สร้างเป็นสารบัญเนื้อหา , สร้างเป็นโครงสร้างองค์กร สร้างเป็นเครือข่ายงาน MLM เป็นต้น
ในตัวอย่างผลลัพธ์ เป็นเพียง การแสดงรายการในลักษณะ โครงสร้าง tree เท่านั้น สามารถนำไป กำหนดการแสดง จัดและตกแต่ง ให้สวยงามได้ด้วย css
ฐานข้อมูลสำหรับ ทดสอบ
/* MySQL Data Transfer Source Host: localhost Source Database: test Target Host: localhost Target Database: test Date: 9/16/2010 4:56:28 PM */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for tbl_category -- ---------------------------- DROP TABLE IF EXISTS `tbl_category`; CREATE TABLE `tbl_category` ( `category_id` int(11) NOT NULL auto_increment, `category_name` varchar(255) NOT NULL, `category_parent` int(11) NOT NULL, PRIMARY KEY (`category_id`) ) ENGINE=MyISAM AUTO_INCREMENT=16 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records -- ---------------------------- INSERT INTO `tbl_category` VALUES ('1', '1', '0'); INSERT INTO `tbl_category` VALUES ('2', '1.1', '1'); INSERT INTO `tbl_category` VALUES ('3', '1.2', '1'); INSERT INTO `tbl_category` VALUES ('4', '1.2.1', '3'); INSERT INTO `tbl_category` VALUES ('5', '2', '0'); INSERT INTO `tbl_category` VALUES ('6', '2.1', '5'); INSERT INTO `tbl_category` VALUES ('7', '2.2', '5'); INSERT INTO `tbl_category` VALUES ('8', '2.1.1', '6'); INSERT INTO `tbl_category` VALUES ('9', '2.1.2', '6'); INSERT INTO `tbl_category` VALUES ('10', '2.2.1', '7'); INSERT INTO `tbl_category` VALUES ('11', '2.2.2', '7'); INSERT INTO `tbl_category` VALUES ('12', '2.2.2.1', '11'); INSERT INTO `tbl_category` VALUES ('13', '2.2.1.1', '10'); INSERT INTO `tbl_category` VALUES ('14', '3', '0'); INSERT INTO `tbl_category` VALUES ('15', '3.1', '14');
ตัวอย่างโค้ด
<?php // ส่วนของการเชิ่อมต่อกับฐานข้อมูล mysql_connect("localhost","root","test") or die("Cannot connect the Server"); mysql_select_db("test") or die("Cannot select database"); mysql_query("set character set utf8"); ?> <?php function mk_array($parentIndex){ // ฟังก์ชันสำหรับสร้าง array จากฐานข้อมูล global $arr_name; // กำหนด ตัวแปร global สำหรับนำข้อมูลไปแสดงในรายการ tree $q="SELECT * FROM tbl_category WHERE category_parent=$parentIndex "; $qr=mysql_query($q); while($rs=mysql_fetch_array($qr)){ $arr_name[$rs['category_id']]=$rs['category_name']; $arr_mk[$rs['category_id']]=mk_array($rs['category_id']); } return $arr_mk; } function tree_list($tree_array) { // ฟังก์ชันสำหรับสร้าง โครงสร้าง แบบ tree จาก array global $arr_name; // กำหนด ตัวแปร global $Output = "<ul>\r\n"; foreach($tree_array as $key => $value) { $Output.= "<li>"; if (is_array($value)) { $Output.= $arr_name[$key].tree_list($value); }else{ $Output.= $arr_name[$key]; } $Output.= "</li>\r\n"; } $Output.= "</ul>\r\n"; return $Output; } $tree_array=mk_array(0); // สร้างตัวแปร array โดย parent เริ่มต้นเท่ากับ 0 echo tree_list($tree_array); // แสดงรายการ โครงสร้าง แบบ tree ?>
เวอร์ชันปรับปรุง
<?php // ส่วนของการเชิ่อมต่อกับฐานข้อมูล mysql_connect("localhost","root","test") or die("Cannot connect the Server"); mysql_select_db("test") or die("Cannot select database"); mysql_query("set character set utf8"); ?> <?php # เวอร์ชัน ปรับปรุง $arr_mk = array(); $q="SELECT * FROM tbl_category "; $qr=mysql_query($q); while($rs=mysql_fetch_array($qr)){ $arr_mk[$rs['category_parent']][$rs['category_id']]=$rs['category_name']; } function tree_list($tree_array,$index) { $Output = ""; if(count($tree_array[$index])){ $Output .= "<ul>\r\n"; foreach($tree_array[$index] as $key => $value) { $Output .= "<li>".$value; $Output .= tree_list($tree_array, $key); $Output .= "</li>\r\n"; } $Output .= "</ul>\r\n"; } return $Output; } echo tree_list($arr_mk , 0); ?>
เพิ่มเติมเนื้อหา ครั้งที่ 1 วันที่ 06-03-2018
อัพเดท กรณีใช้งานกับ mysqli
ไฟล์ dbconnect.php
ไฟล์ demo.php
ไฟล์ dbconnect.php
<?php $mysqli = new mysqli("localhost", "root","","test"); /* check connection */ if ($mysqli->connect_errno) { printf("Connect failed: %sn", $mysqli->connect_error); exit(); } if(!$mysqli->set_charset("utf8")) { printf("Error loading character set utf8: %sn", $mysqli->error); exit(); }
ไฟล์ demo.php
<?php require_once("dbconnect.php"); ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <br> <div style="margin:auto;width:1000px;"> <?php $arr_mk = array(); $sql = " SELECT * FROM tbl_category "; $result = $mysqli->query($sql); if($result && $result->num_rows>0){ // คิวรี่ข้อมูลสำเร็จหรือไม่ และมีรายการข้อมูลหรือไม่ while($row = $result->fetch_assoc()){ // วนลูปแสดงรายการ $arr_mk[$row['category_parent']][$row['category_id']]=$row['category_name']; } } function tree_list($tree_array,$index) { $Output = ""; if(isset($tree_array[$index]) && count($tree_array[$index])){ $Output .= "<ul>rn"; foreach($tree_array[$index] as $key => $value) { $Output .= "<li>".$value; $Output .= tree_list($tree_array, $key); $Output .= "</li>rn"; } $Output .= "</ul>rn"; } return $Output; } echo tree_list($arr_mk , 0); ?> </div> </body> </html>
กด Like หรือ Share เป็นกำลังใจ ให้มีบทความใหม่ๆ เรื่อยๆ น่ะครับ

URL สำหรับอ้างอิง
Top
Copy
ขอบคุณทุกการสนับสนุน
![]()