This article mainly introduces the creation of PHP Infinite-level Tree menu, the main use of recursive functions, interested in the small friends can refer to
Write recursive functions, you can consider caching, define some static variables to save the results of the last run, multi-program run efficiency is very helpful.
The approximate steps are as follows :
Step1: take data to a database, put it into an array,
Step2: Converts the data into a tree-shaped array,
step3: convert this tree-shaped array to HTML code.
You can also combine the second and third steps into one step.
Details are as follows:
1. Database design:
CREATE TABLE ' bg_cate ' (' cate_id ' int () unsigned not NULL auto_increment, ' cate_parentid ' int (0) unsigned DEFAULT ' + ', ' C ' Ate_name ' varchar ' NOT NULL, ' Cate_intro ' varchar ($) Default NULL, ' Cate_order ' int (in) unsigned default ' 0 ', ' cate_ Icon ' varchar ' default null,primary KEY (' cate_id ') engine=myisam default Charset=utf8 auto_increment=34;----Export table Data ' bg_cate '--insert into ' bg_cate ' (' cate_id ', ' cate_parentid ', ' cate_name ', ' Cate_intro ', ' cate_order ', ' Cate_icon ') VALUES (4, 0, ' past events such as Wind ', ' record past ', 0, ' Icons/6.gif '), (5, 0, ' Boiled Three Kingdoms ', ' Wisdom of the Three Kingdoms ', 0, ' Icons/3.gif '), (2, 0, ' technical learning ', ' regular learning of some notes, welcome criticism. 。 ', 0, ' Icons/18.gif '), (3, 0, ' Life drops ', ' record Life drops ', 0, ' Icons/2.gif '), (6, 0, ' Gardenia Blossom ', ' Youth Infinity ', 0, ' Icons/8.gif '), (7, 0, ' holiday casual ', ' Leisurely, comfortable ', 0, ' Icons/24.gif '), (8, 2, ' HTML ', ' HTML learning ', 0, ' Icons/1.gif '), (9, 2, ' CSS ', ' CSS learning ', 0, ' Icons/1.gif '), (ten, 2, ' ph P ', ' PHP learning ', 0, ' Icons/18.gif '), (one, ten, ' PHP Basics ', ' php basics ', 0, ' Icons/1.gif '), (A., ' oop ', ' oop ', 0, ' icons/1.gif '), (+, ' php security ', ' Tell PHP security ', 0, ' Icons/1.gif '), (+, ten, ' Seagull Framework ', ' Seagull Framework ', 0, ' Icons/1.gif '), (2, ' JavaScript ', ' JavaScript learning ', 0, ' Icons/1.gif '), (16, 2, ' design mode ', NULL, 0, ' Icons/1.gif '), (17, 2, ' Software engineering ', ' Software Engineering Learning ', 0, ' Icons/1.gif '), (18, 3, ' Xiamen life ', ' Xiamen life ', 0, ' Icons/8.gif '), (19, 3, ' university Life ', ' university Life ', 0, ' Icons/8.gif '), (20, 3, ' childhood life ', ' childhood ', 0, ' Icons/15.gif '), (21, 19, ' learning ' Learning ', 0, ' Icons/1.gif '), (22, 19, ' movement ', ' sport ', 0, ' Icons/16.gif '), (23, 19, ' travel ', ' travel ', 0, ' Icons/24.gif '), (24, 22, ' volleyball ', ' Volleyball ', 0, ' Icons/9.gif '), (25, 22, ' basketball ', ' basketball ', 0, ' Icons/9.gif '), (26, 22, ' Badminton ', ' badminton ', 0, ' Icons/9.gif '), (27, 22, ' table tennis ', ' ping Pong Ball ', 0, ' icons/9.gif ');
2, to the database to fetch data, put into the array:
Require_once './classes/mydb.php '; $con = Mydb::singleton (); $sql = <<<sql select * from Bg_cate catesql; $data = $c On->getall ($sql);//print_r ($data);
Database operations I use the Pear Class Library, the final $data data format is as follows:
Array ([0] = = Array ([cate_id] = 4 [Cate_parentid] = 0 [Cate_name] = past events such as wind [Cate_intro] and record events [Cate_or Der] + 0 [Cate_icon] = icons/6.gif) [1] = = Array ([cate_id] = 5 [Cate_parentid] + 0 [cate_name] = = Boiled Three Kingdoms [cate_intro] = grade three wisdom [cate_order] = 0 [Cate_icon] = icons/3.gif)
3, the previous step of the data into a tree-like array code as follows:
function Gettree ($data, $pId) {$tree = "; foreach ($data as $k + = $v) {if ($v [' cate_parentid '] = = $pId) {//father finds son $v [' Cat E_parentid '] = Gettree ($data, $v [' cate_id ']); $tree [] = $v; Unset ($data [$k]); }}return $tree;} $tree = Gettree ($data, 0);
The data format for the last output $tree is:
Array ([0] = = Array ([cate_id] = 4 [Cate_parentid] = [Cate_name] = past events such as wind [cate_intro] + = record the past [Cate_orde R] = 0 [Cate_icon] = icons/6.gif) [1] = = Array ([cate_id] = 5 [Cate_parentid] = [cate_name] = water boiled Three Country [Cate_intro] = grade three wisdom [cate_order] = 0 [Cate_icon] = Icons/3.gif) [2] = = Array ([cate_id] = 2 [cate _parentid] = Array ( [0] = = Array ( [cate_id] = 8 [Cate_parentid] = [Cate_name] = > HTML [Cate_intro] + HTML learning [Cate_order] + 0 [Cate_icon] = Icons/1.gif )
4. Convert the tree-like array into HTML code as follows:
function prochtml ($tree) {$html = '; foreach ($tree as $t) {if ($t [' cate_parentid '] = = ') {$html. = "<li>{$t [' Cate_na Me ']}</li> '; } else {$html. = "<li>". $t [' Cate_name ']; $html. = prochtml ($t [' Cate_parentid ']); $html = $html. " </li> "; }}return $html? ' <ul> '. $html. ' </ul> ': $html;} echo prochtml ($tree); The code format of the output HTML is:<ul><li> past such as wind </li><li> water boiling </li><li> Technical learning <ul> <li>html</li> <li>css</li> <li>php <ul> <li>php Basics </li> <li>oop</li> <li>php Safety </li>
5, you can also put the 3rd and 4th step of the code together, the code is as follows:
function Gettree ($data, $pId) {$html = "; foreach ($data as $k + = $v) {if ($v [' cate_parentid '] = = $pId) {//father finds son $html. = "<li>". $v [' Cate_name ']; $html. = Gettree ($data, $v [' cate_id ']); $html = $html. " </li> "; }}return $html? ' <ul> '. $html. ' </ul> ': $html;} Echo gettree ($data, 0);
6. Finally add CSS style, the effect is as follows:
The whole process of thinking is very clear, very suitable for the first time to create an infinite tree-type of friends learning, I hope everyone has a harvest.
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!