阅读:3839回复:0
thinkphp 实现无限极分类//方法一:实现无限极分类 public function indexss(){ Load('extend'); //加载扩展方法 $Category=D('tag_category'); $data['category'] = 'category'; $list=$Category->where($data)->order('category_id asc')->select();//实现同级节点排序 $list=list_to_tree($list,'category_id','parent'); //详细参数见手册 dump($list); } function list_to_tree($list, $pk='id', $pid = 'pid', $child = '_child', $root = 0) { // 创建Tree $tree = array(); if(is_array($list)) { // 创建基于主键的数组引用 $refer = array(); foreach ($list as $key => $data) { $refer[$data[$pk]] =& $list[$key]; } foreach ($list as $key => $data) { // 判断是否存在parent $parentId = $data[$pid]; if ($root == $parentId) { $tree[] =& $list[$key]; }else{ if (isset($refer[$parentId])) { $parent =& $refer[$parentId]; $parent[$child][] =& $list[$key]; } } } } return $tree; } //方法二 public function indexss(){ Load('extend'); //加载扩展方法 $Category=D('tag_category'); $data['category'] = 'category'; $list=$Category->where($data)->order('category_id asc')->select();//实现同级节点排序 $list=list_to_tree($list,'category_id','parent'); //详细参数见手册 $list=$this->findChild($list);//显示全部分类 dump($list); } protected function findChild($arr){ static $tree=array(); foreach ($arr as $key=>$val){ $tree[]=$val; if (isset($val['_child'])){ $this->findChild($val['_child']); } } return $tree; } ab |
|