Write recursive functions, you can consider caching, define some static variables to save the results of the last run, the efficiency of multiple programs is very helpful.
Approximate steps are as follows :
step1: to database fetch data, put into an array,
Step2: Converts the data into a tree-shaped array,
step3: Converts this tree-shaped array into HTML code. The
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 () unsigned DEFAULT ' 0 ' , ' cate_name ' varchar NOT NULL, ' cate_intro ' varchar (+) default NULL, ' Cate_order ' int () unsigned default ' 0 ', ' C
Ate_icon ' varchar (MB) 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 ' (' cate_id ', ' cate_parentid ', ' cate_name ', ' Cate_intro ', ' Cate_order ', ' CA Te_icon ') VALUES (4, 0, ' past like wind ', ' record past ', 0, ' Icons/6.gif ', (5, 0, ' Boiled Three Kingdoms ', ' Grade Three wisdom ', 0, ' Icons/3.gif '), (2, 0, ' technical learning ', ' peacetime learning Learn some of the notes, welcome to criticize correct. ', 0, ' Icons/18.gif '), (3, 0, ' Life drops ', ' record Life drops ', 0, ' Icons/2.gif '), (6, 0, ' Gardenia Bloom ', ' Youth Infinity ', 0, ' Icons/8.gif '), (7, 0, ' Holiday Lounge ' , ' laid-back, comfortable ', 0, ' Icons/24.gif '), (8, 2, ' HTML ', ' HTML learning ', 0, ' Icons/1.gif '), (9, 2, ' CSS ', ' CSS learning ', 0, ' Icons/1.gif '), (10, 2, ' php ', ' php learning ', 0, ' icons/18.gif ', (one, ' PHP basics ', ' php basics ', 0, ' Icons/1.gif '), ((), ' oop ', ' oop ', 0, ' icons/ 1.gif '), (13,Ten, ' PHP security ', ' about PHP security ', 0, ' icons/1.gif ', (+, ' Seagull framework ', ' Seagull Framework ', 0, ' Icons/1.gif '), (15, 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, ' ico Ns/1.gif '), (18, 3, ' Xiamen life ', ' Xiamen Life ', 0, ' Icons/8.gif '), (19, 3, ' College life ', ' College life ', 0, ' Icons/8.gif '), (20, 3, ' childhood life ', ' childhood life ', 0, ' Icons/15.gif '), (21, 19, ' learning ', ' learning ', 0, ' Icons/1.gif '), (22, 19, ' Sport ', ' 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 ', ' table tennis ', 0, ' icons/9.gif ');
Database operations I use the Pear Class Library, and the final $data data format is as follows:
The whole process of thinking is very clear, very suitable for the first time to create an unlimited class of tree-like friends to learn, I hope everyone has harvested.