php 分类代码,Thinkphp无限级分类代码

这篇文章主要介绍了Thinkphp无限级分类代码,无限级分类真的很重要了,我不会写怎么办?本篇就一步步告诉大家如何制作无限级分类,感兴趣的小伙伴们可以参考一下

本篇就一点一点教大家写一个无限级分类出来,其实掌握一个知识,最主要的是要掌握无限级分类的逻辑,那么写起来就很容易的多了。

首先看数据库表:xp_cate

59447b7dabe97e7545db931e6be9665c.png

控制器:CateAction.class.php

class CateAction extends Action{

function index(){

$cate=M('Cate');

$list=$cate->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());

}

}}

?>

模型:CateModel.class.php

class CateModel extends Model{//对应数据库中的表xp_cate

protected $_auto=array(

array('path','tclm',3,'callback'),

);function tclm(){

$pid=isset($_POST['pid'])?(int)$_POST['pid']:0;

echo ($pid);

if($pid==0){

$data=0;

}else{

$list=$this->where("id=$pid")->find();

$data=$list['path'].'-'.$list['id'];//子类的path为父类的path加上父类的id

}

return $data;

}

}

?>

模板:index.html

请选择父级栏目:

根栏目

{$vo['name']}

新的栏目名称:

显示结果如下:

c746a1a99dab32872373c2a2de832a7f.png

有没有掌握无限级分类的逻辑,上文分享的thinkphp无限级分类代码,希望对大家的学习有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值