1.读取数据数据
/** * 所有分类数据 * */ public function categoryData(){ $order = ‘id,sort asc‘; $res = $this->db->cache($this->cache)->order($order)->select(); return $data; }
结构如下
Array ( [1] => Array ( [id] => 1 [pid] => 0 [name] => 新闻40 [desc] => [is_delete] => [add_time] => 1494938181 ) [2] => Array ( [id] => 2 [pid] => 0 [name] => 图片 [desc] => 234234 [is_delete] => [add_time] => 1494938210 ) [3] => Array ( [id] => 3 [pid] => 5 [name] => 234234 [desc] => 234 [is_delete] => [add_time] => 1494938931 ) [4] => Array ( [id] => 4 [pid] => 1 [name] => 234234 [desc] => [is_delete] => [add_time] => ) [5] => Array ( [id] => 5 [pid] => 4 [name] => 第4级分类 [desc] => [is_delete] => [add_time] => ) [6] => Array ( [id] => 6 [pid] => 1 [name] => 12312 [desc] => 3123123 [is_delete] => [add_time] => 1494943856 ) )
2.创建子分类查询
/** * 子分类列表 * */ public function pidData($pid=null){ $result =‘‘; $arr = $this->categoryData(); foreach($arr as $k=>$v){ if($v[‘pid‘] == $pid){ $result[$v[‘id‘]] = $v; } } return $result; }
3.无限循环
/** * 分类树 * */ public function tree($pid=0){ $res = $this->pidData($pid); if(empty($res)) return false; foreach($res as $k=>$v){ $result[$v[‘id‘]] = $v; $rf = $this->tree($v[‘id‘]); if($rf){ foreach($rf as $k=>$vv){ $result[$vv[‘id‘]][‘id‘] = $vv[‘id‘]; $result[$vv[‘id‘]][‘name‘] = " |- ".$vv[‘name‘]; $result[$vv[‘id‘]][‘pid‘] = $vv[‘pid‘]; } } } return $result; }
无限循环结构
Array ( [1] => Array ( [id] => 1 [pid] => 0 [name] => 新闻40 [desc] => [is_delete] => [add_time] => 1494938181 ) [4] => Array ( [id] => 4 [name] => |- 234234 [pid] => 1 ) [5] => Array ( [id] => 5 [name] => |- |- 第4级分类 [pid] => 4 ) [3] => Array ( [id] => 3 [name] => |- |- |- 234234 [pid] => 5 ) [6] => Array ( [id] => 6 [name] => |- 12312 [pid] => 1 ) [2] => Array ( [id] => 2 [pid] => 0 [name] => 图片 [desc] => 234234 [is_delete] => [add_time] => 1494938210 ) )
时间: 2024-10-07 05:00:30