• php递归查找无限分类(树结构)


    一.数据库多次查询形式

    1.原生方式

    header("Content-type:text/html;charset=utf-8");
    $conn = mysql_connect('localhost','root','123456');
    mysql_select_db('test');
    mysql_query("set names 'utf8'");
    function getCate($pid = 0)
    {
        //1.通过父级id获得子级list
        $sql = "select * from cate where pid=".$pid;
        $res = mysql_query($sql);
        if($res)
        {
            while($row = mysql_fetch_assoc($res)){
                $categories[] = $row;
            }
        }
    
        //2.获取某级分类及子分类(无限tree) (重点)
        if(0 < count($categories))
        {
            for($i = 0; $i < count($categories); $i++)
            {
                $categories[$i]['child'] = getCate($categories[$i]['id']);
            }
        }
    
        return $categories;
    }
    
    //测试调用
    $cate = getCate(0);

    2.写在了DAO层

        //通过父级id获得子级list
        public function getCatByPid(int $pid){
            $where  = ['pid'=>$pid,'is_show'=>1];
            $res = $this->getModel()->where($where)->select()->toArray();
            return $res;
        }
    
        //获取某级分类及子分类(无限tree)
        public function getCategory(int $id){
    
            //1.通过父级id获得子级list
            $categories = $this->getCatByPid($id);
    
            //2.获取某级分类及子分类(无限tree) (重点)
            if(count($categories) > 0) {
                for ($i = 0; $i < count($categories); $i++) {
                    $categories[$i]['child'] = $this->getCategory($categories[$i]['id']);
                }
            }
    
            return $categories;
        }

    控制器调用 : $res = $this->service->getCategory(0);

    
    

    二.数据库一次查询出一维数组list,再通过递归生成树结构

    1.原生

    private function tree_data(&$list, $parent){
        $tree = array();
        foreach($list as $row) {
            if($row['permission_parent_id'] == $parent) {
    
                $row['children'] = $this->tree_data($list, $row['permission_id']);
                $tree[] = $row;
            }
        }
        return $tree;
    }

    2.写在了DAO层

        //获得所有分类(不考虑树型接口,再逻辑层处理)
        public function getAll(){
            $where  = ['is_show'=>1];
            //1.查出所有一维数组
            $res = $this->getModel()->where($where)->select()->toArray();
            //2.生成树结构
            $cat_tree = $this->treeData($res, 0);
            return $cat_tree;
    
        }
    
    
        //递归生成树型结构
        function treeData(&$list, $parent_id){
            $tree = array();
            foreach($list as $row) {
                if($row['pid'] == $parent_id) {
    
                    $row['child'] = $this->treeData($list, $row['id']);
                    $tree[] = $row;
                }
            }
            return $tree;
        }

    控制器调用 : $res = $this->service->getAll();//一维数组查找分类全部数据

    转:https://www.cnblogs.com/in-loading/archive/2012/05/24/2516302.html

    https://www.cnblogs.com/chenkg/p/6088259.html

    https://blog.csdn.net/u012767761/article/details/82776969

  • 相关阅读:
    HGOI 20200724
    HGOI 20200722
    [USACO Open08]牛的邻居Cow Neighborhoods解题报告
    [USACO Jan07]考试Schul解题报告
    [CF 249D]Donkey and Start解题报告
    [CF 321D]Ciel and Flipboard解题报告
    [CF 294D]Shaass and Painter Robot解题报告
    [CF 297E]Mystic Carvings解题报告
    [CF 306E]Levko and Game题解翻译
    [CF 316F3]Suns and Rays解题报告
  • 原文地址:https://www.cnblogs.com/fps2tao/p/15203805.html
Copyright © 2020-2023  润新知