PHP implements infinite pole classification _php tutorial

Source: Internet
Author: User

Unlimited pole classification for PHP


Unlimited class classification with two for loops

Table:

Field name field Type Notes Default Value
Id Int Primary KEY Auto-increment
Name varchar Category name
Pid Int Parent class ID 0

The pid of the top category is 0 by default . When we want to take out the sub-classification tree of a classification, the basic idea is recursion , of course, for efficiency issues do not recommend querying the database every time , the general practice is to first remove all the categories, data saved in the PHP array, and then processed , and finally, the results can be cached to increase the efficiency of the next request.

Start by building an original array , which is queried directly from the database:

1. Build Your data

$categories = array(    array('id'=>1,'name'=>'电脑','pid'=>0),    array('id'=>2,'name'=>'手机','pid'=>0),    array('id'=>3,'name'=>'笔记本','pid'=>1),    array('id'=>4,'name'=>'台式机','pid'=>1),    array('id'=>5,'name'=>'智能机','pid'=>2),    array('id'=>6,'name'=>'功能机','pid'=>2),    array('id'=>7,'name'=>'超级本','pid'=>3),    array('id'=>8,'name'=>'游戏本','pid'=>3),);

The goal is to turn it into the following structure
Computers > Notebooks >> Super Ben >> games ben > Desktops
Mobile > Smart devices > Function machines

With an array, you can add a children key to store its sub-categories :

array(    //1对应$categories中的id ,方便直接读取    1 => array(        'id'=>1,        'name'=>'电脑',        'pid'=>0,        children=>array(            &array(                'id'=>3,                'name'=>'笔记本',                'pid'=>1,                'children'=>array(                    //此处省略                )            ),            &array(                'id'=>4,                'name'=>'台式机',                'pid'=>1,                'children'=>array(                    //此处省略                )            ),        )    ),    //其他分类省略)

2. Process:

$tree = array();//第一步,将所有的分类id作为数组key,并创建children单元foreach($categories as $category){    $tree[$category['id']] = $category;    $tree[$category['id']]['children'] = array();}//第二步,利用引用,将每个分类添加到父类children数组中,这样一次遍历即可形成树形结构。foreach ($tree as $key=>$value) {    if ($value['pid'] != 0) {        $tree[$value['pid']]['children'][] = &$tree[$key];    }}print_r($tree);
注:必须通过引用, 否则不会一次遍历就生成最终的结果.

3. Print the results as follows:

Array ([1] = = Array ([id] = 1 [name] + = computer [PID] + 0                            [Children] = = Array ([0] = = Array (                            [ID] + 3 [name] + notebook [pid] = 1                                        [Children] = = Array ([0] = = Array                                            ([id] = 7                                            [Name] = + Super ben [PID] = 3                                                [children] = = Array (                                        )) [1] = = Array         (                                   [ID] + 8 [name] + = game Ben                                                [PID] = 3 [children] = = Array                                        (                                                )                            ))) [1] = = Array (  [id] = 4 [name] + + desktop [pid] = =                        1 [children] = = Array () ))) [2] = = Array ([id] = 2 [name] =&gt ;                        Phone [PID] + 0 [children] = = Array ([0] = = Array      (                      [ID] + 5 [Name] = + Smart machine [PID] + 2                        [children] = = Array ()                            ) [1] = = Array ([id] = 6 [Name] + [PID] = 2 [children] =&gt ;     Array ()))) [3] = = Array ([id] = 3 [name] + notebook [PID] + 1 [chil                            dren] = Array ([0] = = Array (                            [id] = 7 [name] + super Ben [PID] + 3 [Children] = ArRay ()) [1] =&gt ;                            Array ([ID] + 8 [name] + = game Ben                                [PID] = 3 [children] = = Array (  ))) [4] = = Array ([id]                = 4 [name] + + desktop [PID] + 1 [children] + = Array (            )) [5] = = Array ([id] = 5 [Name] = + Smart machine [PID] + 2 [Children] = = Array ()) [6] = = Array ([id] =                > 6 [name] + [PID] = 2 [children] = = Array ( )) [7]= = Array ([id] = 7 [name] + super [PID] + 3 [children] =& Gt Array ()) [8] = = Array ([id] = 8 [name] =&G T Game Ben [PID] = 3 [Children] = Array ()))

Advantages: Clear relationship, modify the relationship between the subordinate simple.

Cons: with PHP processing, if the number of categories is large, the efficiency will be reduced.

http://www.bkjia.com/PHPjc/1036922.html www.bkjia.com true http://www.bkjia.com/PHPjc/1036922.html techarticle PHP implements infinite pole classification using two for loops to implement an infinite level classification table: Field Name fields type notes default ID int primary key auto-increment name varchar classification name PID int parent class ...

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