ตัวอย่าง วิธีการเขียนโค้ด ต่อไปนี้ เป็นแนวทางสำหรับสร้าง รายการแบบ 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
ฐานข้อมูลสำหรับ ทดสอบ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | /* 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' ); |
ตัวอย่างโค้ด
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <?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 ?> |
เวอร์ชันปรับปรุง
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?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); ?> |