Php recursion to implement an infinite classification tree

Source: Internet
Author: User
Php recursive functions implement an infinitely hierarchical tree menu An infinitely hierarchical tree chart is a distinctive feature of an infinitely hierarchical column. let's take a look at two different writing methods.

I. 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 an 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:

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. convert the data from the previous step into a tree-like array
The code 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:

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 a tree-like array into html
The code is 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 output html code format is:

     
    • Past events
    • Three Kingdoms
    • Technology Learning
      • Html
      • Css
      • Php
        • Basic php knowledge
        • Oop
        • Php Security

    5. code integration

    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 .'
    ': $ Html;} echo getTree ($ data, 0 );

    6. add CSS styles

    The second is from the open-source website, which is a very brief introduction.

     Array ('id' => 1, 'pid '=> 0, 'name' => 'jiangxi province'), 2 => array ('id' => 2, 'pid '=> 0, 'name' => 'heilongjiang province'), 3 => array ('id' => 3, 'pid '=> 1, 'name' => 'nanchang city '), 4 => array ('id' => 4, 'pid' => 2, 'name' => 'Harbin '), 5 => array ('id' => 5, 'pid '=> 2, 'name' => 'jixi city '), 6 => array ('id' => 6, 'pid '=> 4, 'name' => 'xiangfang region '), 7 => array ('id' => 7, 'pid '=> 4, 'name' => 'nangang '), 8 => array ('id' => 8, 'pid '=> 6, 'name' =>' and Xinglu '), 9 => array ('id' => 9, 'pid '=> 7, 'name' => 'westbrook street '), 10 => array ('id' => 10, 'pid '=> 8, 'name' => 'Northeast forestry Emy '), 11 => array ('id' => 11, 'pid '=> 9, 'name' => 'Harbin Institute of Technology '), 12 => array ('id' => 12, 'pid '=> 8, 'name' => 'Harbin Normal University '), 13 => array ('id' => 13, 'pid '=> 1, 'name' => 'ganzhou city '), 14 => array ('id' => 14, 'pid '=> 13, 'name' => 'ganxian '), 15 => array ('id' => 15, 'pid '=> 13, 'name' => 'yudu County '), 16 => array ('id' => 16, 'pid '=> 14, 'name' => 'moudian Zhen '), 17 => array ('id' => 17, 'pid' => 14, 'name' => 'tagname '), 18 => array ('id' => 18, 'pid '=> 16, 'name' => 'yiyuan cune '), 19 => array ('id' => 19, 'pid '=> 16, 'name' => 'shangba cune'),); echo"
    "; Print_r (genTree5 ($ items); print_r (genTree9 ($ items); // The latter output format. the former format is similar, but the array key values are different, however, the data structure is not affected./* Array ([0] => Array ([id] => 1 [pid] => 0 [name] => Jiangxi [son] => Array ([0] => Array ([id] => 3 [pid] => 1 [name] => Nanchang City) [1] => Array ([id] => 13 [pid] => 1 [name] => Ganzhou [son] => Array ([0] => Array ([ id] => 14 [pid] => 13 [name] => Ganxian [son] => Array ([0] => Array ([id] => 16 [pid] => 14 [name] => moudian town [son] => Array ([0] => Array ([id] => 18 [pid] => 16 [name] => Yiyuan village) [1] => Array ([id] => 19 [pid] => 16 [name] => Shangba village ))) [1] => Array ([id] => 17 [pid] => 14 [name] => Datian ))) [1] => Array ([id] => 15 [pid] => 13 [name] => Yudu County ))))) [1] => Array ([id] => 2 [pid] => 0 [name] => Heilongjiang [son] => Array ([0] => Array ([ id] => 4 [pid] => 2 [name] => Harbin [son] => Array ([0] => Array ([id] => 6 [pid] => 4 [name] => Xiangfang district [son] => Array ([0] => Array ([id] => 8 [pid] => 6 [name] => hexing Road [son] => Array ([0] => Array ([id] => 10 [pid] => 8 [name] => Northeast Forestry University) [1] => Array ([id] => 12 [pid] => 8 [name] => Harbin Normal University ))))) [1] => Array ([id] => 7 [pid] => 4 [name] => Nangang district [son] => Array ([0] => Array ([ id] => 9 [pid] => 7 [name] => westbound street [son] => Array ([0] => Array ([id] => 11 [pid] => 9 [name] => Harbin Institute of Technology ))))))) [1] => Array ([id] => 5 [pid] => 2 [name] => Jixi city ))))*/

    We can see that the second type of code uses only five lines of code to implement an infinite classification tree, which is worth your reference and learning.

    To learn more about PHP, please stay tuned to PHP.

    Related articles:

    Php uses recursive methods to implement unlimited classification

    Expose the principle of php unlimited classification

    Php unlimited classification implementation method analysis

    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.