作開發,無限分類會被常常用到,一個model直接拿過去結合 str_repeat('','');函數,提高開發速度
<?php namespace app\admin\model; use think\Model; class Cate extends Model { public function cateTree(){ $date=$this->select();//查詢cate表中的數據 $res = $this->cateSort($date); return $res; } //定義一個數組,每循環一條記錄就把它放入該數組並unset該記錄 public function cateSort($data,$pid=0,$count=0){ static $arr=array();//靜態初始化 foreach ($data as $k => $v){ if($v['pid']==$pid){ $v['count']=$count; $arr[]=$v; unset($data[$k]);//去掉再也不使用的 $this->cateSort($data,$v['id'],$count+1); } } return $arr; } }
數據表記錄
無限分類後