首页 > 代码库 > php无限分类二
php无限分类二
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 ) )
php无限分类二
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。