Home>Article>PHP Framework> The use of unlimited categories in ThinkPHP

The use of unlimited categories in ThinkPHP

angryTom
angryTom forward
2020-03-25 13:59:14 3403browse

The example in this article shows how ThinkPHP auto-fills to achieve infinite classification. It is one of the common functions of ThinkPHP and is of great practical value. Now I will share the complete example with you for your reference.

The use of unlimited categories in ThinkPHP

Use of ThinkPHP unlimited classification

The specific implementation steps are as follows (this article uses version 3.1.3 of the TP framework):

(Recommended tutorial:thinkphp tutorial)

Step one: The table aoli_cate is as shown below

CREATE TABLE `fenlei` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键', `name` varchar(255) DEFAULT NULL COMMENT '父id', `pid` int(255) NOT NULL COMMENT '名字', `path` varchar(20) DEFAULT NULL,//path字段是给数据排序用 PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=utf8; insert into fenlei(id,name,pid,path) values (1,’新闻’,0,’0’), (2,’中国新闻’,1,’0-1’),//path 最后一个数据是排序用的 (3,’美国新闻’,1,’0-1’), (4,’北京新闻’,2,’0-1-2’), (5,’华盛顿新闻’,3,’0-1-3’), (6,’日本新闻’,1,’0-1’);

The use of unlimited categories in ThinkPHP

Step 2: action part

aoli/Home/Lib/Action/CataAction.class.php file is as follows:

field("id,name,pid,path,concat(path,'-',id) as bpath")->order('bpath')->select(); foreach($list as $key=>$value){ //在查询获取的数组里面增加一个'count'数组; //统计bpath字段的字符串个数,并赋值给'count'数组。 $list[$key]['count']=count(explode('-',$value['bpath'])); } //展示数据 $this->assign('alist',$list); $this->display(); } //添加栏目 function add(){ //实例化CateModel类 $cate=new CateModel(); //如果从CateModel模型接受数据成功 if($vo=$cate->create()){ //执行添加 if($cate->add()){ $this->success('添加栏目成功'); }else{ $this->error('添加栏目失败'); } //dump($vo); }else{ //返回错误信息 $this->error($cate->getError()); } } } ?>

Step 3: Model part

aoli/Home/Lib/Model/CataModel.class.php file is as follows:

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

Step 4: viewView part

 
//size="20"可以将select 框变大 请选择父级栏目:
新的栏目名称:

The above is the detailed content of The use of unlimited categories in ThinkPHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete