This article describes how to create an infinitely hierarchical tree menu in php, mainly using recursive functions. if you are interested, refer to writing recursive functions and consider caching, defining static variables to store the results of the previous operation is helpful for multiple programs to run efficiently ..
The procedure is as follows::
Step 1:Fetch data from the database and put it in an array,
Step 2:Convert data into a tree-like array,
Step 3:Convert the tree-like array into html code.
You can also combine step 2 and step 3.
The details are as follows:
1. database design:
Create table 'bg _ cate '('Cate _ id' int (30) unsigned not null AUTO_INCREMENT, 'Cate _ parentid' int (30) unsigned DEFAULT '0 ', 'Cate _ name' varchar (100) not null, 'Cate _ intro' varchar (500) default null, 'Cate _ order' int (30) unsigned DEFAULT '0 ', 'Cate _ Icon 'varchar (100) default null, primary key ('Cate _ id') ENGINE = MyISAM default charset = utf8 AUTO_INCREMENT = 34; ---- export the data in the table 'bg _ cate' -- insert into 'bg _ cate' ('C Ate_Id ', 'Cate _ parentid', 'Cate _ name', 'Cate _ intro', 'Cate _ order', 'Cate _ Icon') VALUES (4, 0, 'past events like wind', 'past events recorded ', 0, 'icons'/6.gif'), (5, 0, 'boiled Three Kingdoms ', 'Taste three kingdoms wis', 0, 'icons'/3.gif '), (2, 0, 'technical learn', 'Notes of study at ordinary times, please criticize and correct. ', 0, 'icons'/18.gif'), (3, 0, 'Life drive', 'recording life drive', 0, 'icons'/2.gi'), (6, 0, 'gardenia Kai', 'Youth limited', 0, 'icons'/8.gif '), (7, 0, 'holiday casual', 'leisurely, comfortable ', 0, 'icons'/24.gif '), (8, 2, 'html', 'HTML learn', 0, 'icons'/1.gif'), (9, 2, 'css ', 'css learn', 0, 'icons'/1.gif '), (10, 2, 'php', 'php learn', 0, 'icons'/18.gif'), (11, 10. 'php basics ', 'php basics', 0, 'icons'/1.gif'), (12, 10, 'Oop ', 'Oop', 0, 'icons'/1.gif '), (13, 10, 'php security', 'php security', 0, 'icons'/1.gif'), (14, 10, 'seagull framework', 'Seagull framework', 0, 'Icons/1.gif '), (15, 2, 'javascript', 'javascript learn', 0, 'icons'/1.gif '), (16, 2, 'design mode', NULL, 0, 'icons'/1.gif'), (17, 2, 'software engine ', 'Software Engineering learn', 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 life ', 0, 'icons'/15.gif '), (21, 19, 'learn', 'learn', 0, 'icons'/1.gif'), (22, 19, 'motor ', 'Sports ', 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', 'table tennis ', 0, 'icons'/9.gif ');
2. fetch data from the database and put it in the array:
require_once './classes/MyDB.php';$con = MyDB::singleton();$sql = <<
getAll($sql);//print_r($data);
I use the pear class library for database operations. The final $ data format is as follows:
Repeated Array ([0] => Array ([cate_Id] => 4 [cate_ParentId] => 0 [cate_Name] => past events such as wind [cate_Intro] => Record past events [cate_Order] => 0 [cate_Icon] => icons/6.gif) [1] => Array ([cate_Id] => 5 [cate_ParentId] => 0 [cate_Name] => boiled three kingdoms [cate_Intro] => taste three kingdoms wisdom [cate_Order] => 0 [cate_Icon] => icons/3.gif)
3. the code for converting data from the previous step into a tree-like array is as follows:
Function getTree ($ data, $ pId) {$ tree = ''; foreach ($ data as $ k => $ v) {if ($ v ['Cate _ parentid'] = $ pId) {// The father finds the son $ v ['Cate _ parentid'] = getTree ($ data, $ v ['Cate _ id']); $ tree [] = $ v; // unset ($ data [$ k]);} return $ tree ;} $ tree = getTree ($ data, 0 );
The data format of the output $ tree is as follows:
Repeated Array ([0] => Array ([cate_Id] => 4 [cate_ParentId] => [cate_Name] => past events such as wind [cate_Intro] => Record past events [cate_Order] => 0 [cate_Icon] => icons/6.gif) [1] => Array ([cate_Id] => 5 [cate_ParentId] => [cate_Name] => Three Kingdoms [cate_Intro] => Three Kingdoms wisdom of taste [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. ="
{$ T ['Cate _ name']}";} Else {$ html. ="
". $ T ['Cate _ name']; $ html. = procHtml ($ t ['Cate _ parentid']); $ html = $ html ."";}} Return $ html? '
': $ Html;} echo procHtml ($ tree); the output html code format is:
- Past events
- Three Kingdoms
- Technology Learning
- Html
- Css
- Php
- Basic php knowledge
- Oop
- Php Security
5. You can also combine steps 3rd and 4th with the following code:
Function getTree ($ data, $ pId) {$ html = ''; foreach ($ data as $ k => $ v) {if ($ v ['Cate _ parentid'] = $ pId) {// find your son $ html. ="
". $ V ['Cate _ name']; $ html. = getTree ($ data, $ v ['Cate _ id']); $ html = $ html ."";}} Return $ html? '
': $ Html;} echo getTree ($ data, 0 );
6. add a css style. The effect is as follows:
The entire process is very clear, and it is very suitable for the first time to create an unlimited tree for friends to learn, I hope everyone will have some gains.