PHP Recursive implementation of infinite-level classification tree

Source: Internet
Author: User
Tags php basics
The infinite-level tree can be said to be a significant feature of the infinite-level column, and we'll look at two different ways to do it next.

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

Two. Take the data to the database and put it into the array.

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

Database operations I'm using 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] [           cate_order] = 0           [Cate_icon] = icons/6.gif       )   [1] = = Array       ( C13/>[CATE_ID] + 5           [Cate_parentid] = 0           [cate_name] + boiled Three Kingdoms           [Cate_intro] = grade three Wisdom           [ Cate_order] + 0           [Cate_icon] = Icons/3.gif       )

Three. Convert the data from the previous step to a tree-shaped array
The code is as follows:

function Gettree ($data, $pId) {$tree = "; foreach ($data as $k + = $v) {  if ($v [' cate_parentid '] = = $pId)  {        // Father found son   $v [' cate_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] = = Go to  Things like wind [Cate_intro] and 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 three wisdom [cate_order] = 0 [Cate_icon] = Icons/3.gif) [2] = ARR Ay ([cate_id] = 2 [Cate_parentid] = Array ([0] = A                           Rray ([cate_id] = 8 [Cate_parentid] =                           [Cate_name] + html [cate_intro] = HTML Learning [Cate_order] = 0 [Cate_icon] = icons/1.gif)

Four. Converting a tree-shaped array to HTML
The code is as follows:

function prochtml ($tree) {$html = '; foreach ($tree as $t) {  if ($t [' cate_parentid '] = = ')  {   $html. = "<li& Gt {$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 for the output HTML is:

<ul><li> past events such as wind </li><li> boiling water </li><li> Technical Learning  <ul>   <li>html </li>   <li>css</li>   <li>php    <ul>     <li>php Basics </li>     <li>oop</li>     <li>php Safety </li>

Five. Code integration

function Gettree ($data, $pId) {$html = "; foreach ($data as $k + = $v) {  if ($v [' cate_parentid '] = = $pId)  {        // Father found 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);

Six. Add CSS style

The second is a very, very brief introduction from the open Source website.

<?php function GenTree5 ($items) {foreach ($items as $item) $items [$item [' pid ']][' son '] [$item [' id ']] = &amp     ; $items [$item [' id ']]; return Isset ($items [0][' son '])? $items [0][' son ']: Array ();  /** * Format data into a tree structure * @author xuefen.tong * @param array $items * @return array */function genTree9 ($items) {$tree = Array (); Well-formatted tree foreach ($items as $item) if (Isset ($items [$item [' pid]]) $items [$item [' pid ']][' son '] [] = &        amp; $items [$item [' id ']];    else $tree [] = & $items [$item [' id ']]; return $tree;} $items = Array (1 = = Array (' id ' = = 1, ' pid ' = 0, ' name ' = ' Jiangxi '), 2 = = Array (' id ' = = 2, ' pid ' =& Gt  0, ' name ' = ' Heilongjiang Province '), 3 = = Array (' id ' = 3, ' pid ' = = 1, ' name ' = ' Nanchang '), 4 = = Array (' id ' = = 4, ' pid ' = 2, ' name ' = ' Harbin '), 5 = = Array (' id ' = 5, ' pid ' = = 2, ' name ' = ' Jixi '), 6 = = Array ( ' id ' = 6, ' pid ' = 4, ' name ' = ' Xiangfang District '), 7 = ArraY (' id ' = = 7, ' pid ' = + 4, ' name ' = ' Nangang '), 8 = = Array (' id ' = = 8, ' pid ' = 6, ' name ' = ' and Hing Road '), 9 = = Array (' id ' = = 9, ' pid ' = + 7, ' name ' = ' + ' West Dazhi Street '), ' ten ' = ' array ' (' id ' = ' = ', ' pid ' = ' 8 ', ' name ' =& Gt  ' Northeast Forestry University '), one-by-one and array (' id ' = = ', ' pid ' = 9, ' name ' = ' Harbin Institute of Technology '), and array (' id ' = ' + ', ' pid ') + 8, ' name ' = ' harbin Normal University '), and the array (' id ' = ' + ', ' pid ' = 1, ' name ' = ' Ganzhou '), + = Array (    ' id ' = +, ' pid ' = ' + ', ' name ' = ' Gan County '), and the array (' id ' = +, ' pid ' = ' + '), ' name ' = ' Yudu County '), + = array (' id ' = ' + ', ' pid ' = ' + ', ' name ' = ' Mao town '), ' + ' array ' (' id ' = ' = ', ' pid ' = ', ' Nam E ' = ' Ota township '), the array (' id ' = ' + ', ' pid ' = + ', ' name ' = ' righteousness source village '), + = array (' id ' = ' p ') ID ' = +, ' name ' = ' Shang Ba village '), echo "<pre>";p Rint_r (GenTree5 ($items));p Rint_r (GenTree9 ($items)); The latter output format, the former similar, but the array key value is not the same, but does not affect the data structure/*array ([0] = = Array ([id] = 1 [PID] + 0 [name] + Jiangxi [son] + = array ([0] = = Array ([id] = 3 [P                    ID] + 1 [name] = Nanchang) [1] = = Array                        ([id] [PID] + 1 [name] = = Ganzhou                                    [Son] = array ([0] = = Array ([id] = [p ID] = [name] = Gan County [son] = Arr                                        Ay ([0] = = Array        ([id] = 16                                                    [PID] = [name] + Mao Dian Zhen                                                        [son] = = Array (                                                            [0] = = Array (  [id] = [pid] =                                                            [Name] * + Righteousness Source Village                                                            ) [1] = = Array                                            ([id] = 19                [PID] [[Name] = + Shangba Village                                                 )                                                         )                                                ) [1] = = Array                                                    ([id] = 17                                                [PID] = [name] = Ota Township                                 )                                             )                                     )                                         [1] = = Array ([id] = 15                                    [PID] [name] = = Yudu County)))) [1] = = Array ([id] = 2                    [PID] = 0 [name] = Heilongjiang Province [son] + array ([0] = = Array                        ([id] = 4 [PID] + 2 [name] = + Harbin City                                [Son] = array ([0] = = Array                                    ([ID] + 6 [pid] = 4                                        [name] = = Xiangfang District [son] = Array                                                ([0] = = Array (                          [id] = 8 [PID] + 6                      [Name] = + Hing Road [son] = = Array                                                            ([0] = = Array                                                            ([id] = 10                                                            [PID] = 8                                                            [Name] = Northeast Forestry University                                                            ) [1] = = Array                                                            ([id] = 12 [PID] = 8 [n              AME] =                                               Harbin Normal University)                                         )                                             )                                )) [1] = = Array                                    ([ID] + 7 [PID] = 4                                        [Name] = Nangang District [son] + Array                                            ([0] = = Array (                                            [id] = 9 [PID] + 7                                                [Name] = West Big Straight Street [son] + Array     (                                           [0] = = Array (                                                    [id] = [pid] = 9                                                     [Name] = Harbin Institute of Technology                                             )                                                 )                             )                                         )                                 )                         )) [1] = = Array ([id] = 5 [PID] = 2 [name] = Jixi))) */

It can be seen that the second kind of code only uses 5 lines of code to achieve an infinite class of classification tree, it is worth everyone's reference and learning.

For more information about PHP, please continue to follow topic.alibabacloud.com

"More Class Library downloads"

1. php page Library Download 2. PHP Image processing Class Library 3. Verification Code Class library download 4. File Upload class Library 5. String processing class
6. Classification Library 7. Database Operations Class Library 8. IP class Library 9. Cache class Library 10. Template Engine Class library

Related articles:

PHP uses recursive method to realize infinite class classification

Uncover the principles of PHP infinite class classification

Analysis of implementation method of PHP infinite class classification

  • 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.