PHP Create infinite-level tree menu, PHP Create-tree-_php Tutorial

Source: Internet
Author: User
Tags php basics

PHP Create infinite-level tree menu, PHP Create-tree


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 = <<
 
  
   
  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_o Rder] + 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_ord ER] = 0 [Cate_icon] = icons/6.gif) [1] = = Array ([cate_id] = 5 [Cate_parentid] = [cate_name] = water boiled Three Kingdoms [Cate_intro] = grade three wisdom [cate_order] = 0 [Cate_icon] = Icons/3.gif) [2] = = Array ([cate_id] + 2 [cat E_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. '
    : $html;} echo prochtml ($tree); The code format of the output HTML is:
    • The past is like the wind
    • Three Kingdoms of boiled water
    • Technical Learning
      • Html
      • Css
      • Php
        • PHP Basics
        • Oop
        • PHP Security

    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. = "
  • ". $v [' Cate_name ']; $html. = Gettree ($data, $v [' cate_id ']); $html = $html. "
  • "; }} Return $html? "
      . $html. '
    : $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.

    http://www.bkjia.com/PHPjc/1068824.html www.bkjia.com true http://www.bkjia.com/PHPjc/1068824.html techarticle PHP creates an infinite-level tree menu, PHP Create-level tree write recursive function, you can consider caching, define some static variables to save the results of the last run, multi-program running efficiency is very helpful. Big ...

  • Related Article

    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.