thinkphp无限极分类实现方法

大家讲道理
Release: 2023-03-05 21:40:02
Original
4701 people have browsed it

本文讲解了thinkphp框架下实现无限级分类的方法,无限级分类一般应用在网站的分类菜单中,是很常用的数据结构和功能,在thinkphp中实现这种方法也是很容易的一件事,接下来我们就来学习下如何来使用。

无限分类原理是添加一个字段(比如Sid)作区分,顶级分类Sid为0,二级分类Sid为上一级分类的ID,一次类推。输出的时候一般使用递归即可。

我们首先来新建一张数据表,表结构如下:

QQ截图20170221095610.png

控制器: 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());
 }
 }}
 ?>
Copy after login

模型:CateModel.class.php

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

模板:index.html

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

显示结果如下:

51b28cd277904.jpg


注:

本文所实现的无限级分类使用了ThinkPhP框架。也就是采用的MVC架构,其中控制器,模版和模型层写的很清楚,对于会使用TP的来说理解很容易,如果有对TP框架不理解的同学,可以先了解下框架的使用方法,在回头看我们的写法。

source:php.cn
Statement of this Website
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 [email protected]
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!