本文实例展示了ThinkPHP自动填充实现无限级分类的方法,是ThinkPHP常用功能之一,非常具有实用价值。现将完整实例分享给大家,供大家参考。具体实现步骤如下:
表aoli_cate如下图所示:
一、action部分:
aoli/Home/Lib/Action/CataAction.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('添加栏目失败'); } //dump($vo); }else{ $this->error($cate->getError()); } } }?>
二、模型部分:
aoli/Home/Lib/Model/CataModel.class.php文件如下:
where("id=$pid")->find(); $data=$list['path'].'-'.$list['id'];//子类的path为父类的path加上父类的id } return $data; }}?>
三、view视图部分
aoli/Home/Tpl/default/Cate/index.html页面如下:
请选择父级栏目: <div style="color:transparent">本&文来源gaodai^.ma#com搞#代!码网</div><strong>搞gaodaima代码</strong> 根栏目 for($i=0;$i<$vo['count'];$i++){ echo ' '; } {$vo['name']} <br /> 新的栏目名称:<br />
相信本文所述实例对大家ThinkPHP程序设计有一定的参考价值。