• ThinkPHP


    得到数据:

    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();
    }

    效果:

     

  • 相关阅读:
    洛谷 P5057 [CQOI2006]简单题 题解
    洛谷 P3368 【模板】树状数组 2 题解
    洛谷 P3374 【模板】树状数组 1 题解
    洛谷 P2023 [AHOI2009]维护序列 题解
    洛谷 P2253 好一个一中腰鼓! 题解
    求最长不下降/上升/下降/不上升子序列
    [SQL Server]Index/deadlock
    Ubuntu 14.04下从源码安装qt4.x
    Ubuntu系统下Import cv2提示no modules ...错误
    Ubuntu 14.04下安装CUDA8.0
  • 原文地址:https://www.cnblogs.com/KTblog/p/5203226.html
Copyright © 2020-2023  润新知