该代码在Model模型中,首先创建一个名为***Model.class.php的文件,然后创建一个catetree()的方法;
public function catetree() { $data=$this->order('sort asc')->select(); return $this->resort($data); } public function resort($data,$parentid=0,$level=0) { static $ret=array(); foreach ($data as $k => $v) { if($v['parentid']==$parentid) { $v['level']=$level; $ret[]=$v; $this->resort($data,$v['id'],$level+1); } } return $ret; }
控制器中的代码,控制器以***Controller.class.php文件命名
public function lst() { $cate = D('cate'); $cates = $cate->catetree(); $this->assign('cates', $cates); $this->display(); }