thinkPHP实现无限级别分类

jerry thinkphp 2015年11月19日 收藏
thinkPHP实现无限级别分类,用递归方法
代码如下:
  1. public $cateListAll = array();
  2. public function editCate()
  3. {
  4.     $this->cateChildList(0,$nb);//从父级=0开始递归
  5.     $this->cateList=$this->cateListAll;
  6.     $this->display();
  7. }
  8.     
  9. //无限极分类
  10. protected function cateChildList($pid,$nb)
  11. {
  12.     $cate=M('cate');
  13.     $parent=$cate->where('parent_id="'.$pid.'"')->order('sort asc,id desc')->select();
  14.     if($parent)
  15.     {
  16.         $nb = $nb."  ";
  17.         foreach($parent as $item)
  18.         {
  19.             $item['name']=$nb.'├ '.$item['name'];
  20.             $this->cateListAll[]=$item;
  21.             $this->cateChildList($item['id'],$nb);
  22.         }
  23.     }
  24. }
其中前台html直接循环cateList即可

效果如下: