Home  >  Article  >  Backend Development  >  thinkphp infinite classification implementation method

thinkphp infinite classification implementation method

大家讲道理
大家讲道理Original
2017-02-21 10:09:044717browse

This article explains the method of implementing infinite classification under the thinkphp framework. Infinite classification is generally used in the classification menu of websites. It is a very common data structure and function. It is also very easy to implement this method in thinkphp. Okay, let's learn how to use it next.

The principle of infinite classification is to add a field (such as Sid) for differentiation. The top-level classification Sid is 0, the second-level classification Sid is the ID of the upper-level classification, and so on. When outputting, recursion is generally used.

Let’s first create a new data table with the following structure:

thinkphp infinite classification implementation method

Controller: CateAction .class.php

field("id,name,pid,path,concat(path,'-',id) as bpath")->order('bpath')->select();
 foreach($list as $key=>$value){
 $list[$key]['count']=count(explode('-',$value['bpath']));
 }
 $this->assign('alist',$list);
 $this->display();
 }//添加栏目
 function add(){
 $cate=new CateModel();if($vo=$cate->create()){
 if($cate->add()){
 $this->success('添加栏目成功');
 }else{
 $this->error('添加栏目失败');
 }
 }else{
 $this->error($cate->getError());
 }
 }}
 ?>

Model: CateModel.class.php

where("id=$pid")->find();
 $data=$list['path'].'-'.$list['id'];//子类的path为父类的path加上父类的id
 }
 return $data;
 }
 }
 ?>

Template :index.html

请选择父级栏目:
新的栏目名称:

The display results are as follows:

thinkphp infinite classification implementation method


##Note:

The infinite-level classification implemented in this article uses the ThinkPhP framework. That is to say, the MVC architecture is adopted, in which the controller, template and model layers are clearly written. It is easy to understand for those who can use TP. If there are students who do not understand the TP framework, you can first understand how to use the framework. Looking back at our writing style.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn