码迷,mamicode.com
首页 > Web开发 > 详细

php无限分类二

时间:2017-05-17 14:00:14      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:esc   res   als   turn   delete   each   category   取数据   array   

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无限分类二

标签:esc   res   als   turn   delete   each   category   取数据   array   

原文地址:http://www.cnblogs.com/wesky/p/6866843.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!