得到数据:
Array ( [0] => Array ( [id] => 5 [name] => PHP [pid] => 0 [sort] => 1 [blog] => ) [1] => Array ( [id] => 2 [name] => DIV+CSS [pid] => 0 [sort] => 2 [blog] => Array ( [0] => Array ( [id] => 1 [title] => 这是一条测试文章 [time] => 1455717047 ) ) ) [2] => Array ( [id] => 3 [name] => JavaScript [pid] => 0 [sort] => 3 [blog] => Array ( [0] => Array ( [id] => 2 [title] => jquery博文 [time] => 1455763110 ) [1] => Array ( [id] => 4 [title] => 博文博文 [time] => 1455869366 ) ) ) [3] => Array ( [id] => 4 [name] => Apache [pid] => 0 [sort] => 4 [blog] => ) [4] => Array ( [id] => 1 [name] => HTML [pid] => 0 [sort] => 5 [blog] => ) [5] => Array ( [id] => 6 [name] => Linux [pid] => 0 [sort] => 6 [blog] => ) [6] => Array ( [id] => 7 [name] => 其他 [pid] => 0 [sort] => 7 [blog] => ) )
代码:
Public function index() { //获取所有父类分类 $FaCate = M('Cate')->where(array('pid'=>0))->order('sort')->select(); //获取所有分类 $allCate = M('Cate')->order('sort')->select(); // 引入分类类文件 import('Class.Category', APP_PATH); $db = M('Blog'); $field = array('id', 'title', 'time');//取出的字段 foreach ($FaCate as $key => $value) { //根据父类id,获取子类id $cids = Category::getChildsId($allCate, $value['id']); //连同父类id一起放入数组,输出的结果是,父类id以及其下的所有子类id $cids[] = $value['id']; //根据查询出的id,查找博文表blog,获取博文数据,将博文数据放入Facate数组中的blog子数组中。 $whe = array('cid'=>array('IN', $cids));//博文表中的查询条件,cid IN ('', '', '') //将查询除的数据放入blog数组中 $FaCate[$key]['blog'] = $db->field($field)->where($whe)->select();//不能进行排序 } print_r($FaCate);die; $this->cate = $FaCate; $this->display(); }
效果: