PHP Create infinite-level tree menu _php Tips

Source: Internet
Author: User

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 ');

2, to the database to take data, put to the array:

Require_once './classes/mydb.php ';
$con = Mydb::singleton ();
$sql = <<<sql
 select * from bg_cate cate
sql;
$data = $con->getall ($sql);
Print_r ($data);

Database operations I use the Pear Class Library, and the final $data data format is as follows:

Array
(
 [0] => array
 (
 [cate_id] => 4
 [Cate_parentid] => 0
 [Cate_name] => The past is like wind
 [Cate_intro] => record the past
 [Cate_order] => 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 is as follows:

function Gettree ($data, $pId)
{
$tree = ';
foreach ($data as $k => $v)
{
 if ($v [' cate_parentid '] = = $pId)
 {//father finds son
 $v [' cate_parentid '] = Gettree ($data, $v [' cate_id ']);
 $tree [] = $v;
 Unset ($data [$k]);
 }
return $tree;
}
$tree = Gettree ($data, 0);

The final output $tree data format is:

Array
(
 [0] => array
 (
 [cate_id] => 4
 [Cate_parentid] =>
 [Cate_name] => the past as the wind
 [Cate_intro] => record the past
 [Cate_order] => 0
 [Cate_icon] => icons/6.gif
 )
 [1] => Array
 (
 [cate_id] => 5
 [Cate_parentid] =>
 [cate_name] => boiled Three Kingdoms
 [Cate_intro] => grade wisdom of the Three Kingdoms
 [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, 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_name ']}</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 wind </li>
<li> boiled three </li>
<li> Technology study
 <ul>
 <li>html</li>
 <li>css</li>
 <li>php
 < Ul>
 <li>php Basic knowledge </li>
 <li>oop</li>
 <li>php Safety </li>

5, you can also put the code of steps 3rd and 4th together, the code is as follows:

function Gettree ($data, $pId)
{
$html = ';
foreach ($data as $k => $v)
{
 if ($v [' cate_parentid '] = = $pId)
 {//father finds his 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, and finally add some CSS style, the effect 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.