Create an infinite-polar classification tree structure operation method

Source: Internet
Author: User
First on

Top class classification is actually a first-class classification, two-level classification is also called sub-classification classification, on this basis, sub-classification can also have sub-classification, which constitutes an infinite pole classification.

Next look at the implementation of the Code:

First, in the Controller by the field query, query out all the classification information (ID: The ID value of the classification, Cate_name: the name of the classification, PID: Parent id,sorts: Prepare to display the order of headings, can not be written. )

1 Public     function Cate_display () 2     {3         $cate = D (' Cate '); 4         $field = array (' id ', ' cate_name ', ' pid ', ' Sorts '); 5         $list = $cate->allcategory ($field), 6         $this->assign (' list ', $list); 7         $this->display (); 8     }

Second, the code in the model

Create two methods in a model corresponding to a controller

1. Query all classification information and call the Generate classification tree method:

1 public Function allcategory ($field = ' * ') {2         $data = $this->field ($field)->select (); 3         return $this- >tree ($data); 4     }

2. Generate a classification tree (using recursion, passing in data, and pid[parent class id],level[layer, used to control the display of the-quantity] Two variables, the initial value is 0)

1 public Function tree ($data, $pid =0, $level =0) {2         static $tree = Array (); 3         foreach ($data as $k + = $v) {4             if ( $v [' pid '] = = $pid) {5                 $v [' level '] = $level; 6                 $tree []= $v; 7                 $this->tree ($data, $v [' id '], $level + 1); 8             } 9         }10         return $tree;     

Third, the code in the view file

1 <div class= "Form-group" > 2                 <label for= "pid" class= "Col-sm-2 Control-label no-padding-right" > Parent Menu </label> 3                 <div class= "col-sm-6" > 4                     <select name= "pid" style= "width:100%;" > 5                         <option selected= "selected" value= "0" > Top menu </option> 6                         <volist name= "Row" id= "Val" >   7                             <option value= "{$val. ID}" ><?php echo str_repeat ('-', $val [' Level ']*4); >{$val. Cate_name} 8                             </option>   9                         </volist>                     </select>11                 </div>12             </ Div>

In this way, an infinite recursive classification of the tree structure is completed, summary: The core idea is the recursive function in the model, first passed in the PID default is zero, the next time the recursive PID is the superior Id,level used to record the recursive layer, and finally in the View page display, call PHP built-in function str _repeat (), used to repeat output '-' in order to achieve the output to differentiate the effect of the series.

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.