Create an infinitus classification tree structure and a Classification Tree Structure

Source: Internet
Author: User

Create an infinitus classification tree structure and a Classification Tree Structure

First

A top-level category is actually a level-1 category. A level-2 category is also called a sub-category of a level-1 category. On this basis, a sub-category can also have sub-categories, thus forming an infinitus category.

 

Next, let's look at the specific implementation code:

1. query all categories by field in the Controller (id: ID of the category, cate_name: name of the category, pid: parent ID, sorts: prepare for sorting the display titles in sequence. Do not write .)

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     }

Ii. Code in the Model

Create two methods in the model of the corresponding Controller

1. query all category information and call the method for generating the category tree:

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

2. Generate a category tree (recursively, pass in data, and pid [parent class id], level [layers, used to control the displayed-quantity] two variables, the initial value is zero)

 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     11         return $tree;12     }

3. Code in the View File

1 <div class = "form-group"> 2 <label for = "pid" class = "col-sm-2 control-label no-padding-right"> superior menu </label> 3 <div class = "col-sm-6"> 4 <select name = "pid"> 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> 10 </select> 11 </div> 12 </div>

 

In this way, a tree-like structure of classification that can be infinitely recursive is completed. Conclusion: The core idea is to use recursive functions in the model. The default value of pid is zero, the pid passed in each recursion is the superior id. level is used to record the number of recursive layers. When the view page is displayed, the PHP built-in function str_repeat () is called (), it is used to repeatedly output '-' to achieve the effect of the number of output time zones.

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.