无限级分类--1(商品分类)

无限级分类多用于商品的分类

其原理就是调用函数体本身,通过传递的参数不同,则查出的数据不同

无限级分类对表的设计有着要求,子类中必须有父类的id,如果是顶级分类id则父级id(parent_id)的值为0

表字段:

 cat_id(自增id)     cat_name(名称)    parent_id(父类id)

代码实现:


    //无限级分类
    public function always_category($path=0)
    {
        $data = Db::table('category')->field(['cat_id','cat_name','parent_id'])->where(['parent_id'=>$path])->select();
        foreach($data as $key=>$val)
        {
            $son = $this->always_category($val['cat_id']);
            if(!empty($son))
            {
                $data[$key]['son'] = $son;
            }
        }
        return $data;
    }

查出的结果格式如下:

 Array
(
    [0] => Array
        (
            [cat_id] => 28
            [cat_name] => 手机、数码
            [parent_id] => 0
            [son] => Array
                (
                    [0] => Array
                        (
                            [cat_id] => 29
                            [cat_name] => 手机通讯
                            [parent_id] => 28
                            [son] => Array
                                (
                                    [0] => Array
                                        (
                                            [cat_id] => 30
                                            [cat_name] => 618手机
                                            [parent_id] => 29
                                        )

                                    [1] => Array
                                        (
                                            [cat_id] => 31
                                            [cat_name] => 手机
                                            [parent_id] => 29
                                        )

                                    [2] => Array
                                        (
                                            [cat_id] => 32
                                            [cat_name] => 对讲机
                                            [parent_id] => 29
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [cat_id] => 33
                            [cat_name] => 数码配件
                            [parent_id] => 28
                            [son] => Array
                                (
                                    [0] => Array
                                        (
                                            [cat_id] => 34
                                            [cat_name] => 储存卡
                                            [parent_id] => 33
                                        )

                                    [1] => Array
                                        (
                                            [cat_id] => 35
                                            [cat_name] => 读卡器
                                            [parent_id] => 33
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [cat_id] => 36
            [cat_name] => 图书、音影
            [parent_id] => 0
            [son] => Array
                (
                    [0] => Array
                        (
                            [cat_id] => 38
                            [cat_name] => 教育
                            [parent_id] => 36
                            [son] => Array
                                (
                                    [0] => Array
                                        (
                                            [cat_id] => 41
                                            [cat_name] => 考试
                                            [parent_id] => 38
                                        )

                                    [1] => Array
                                        (
                                            [cat_id] => 42
                                            [cat_name] => 外语学习
                                            [parent_id] => 38
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [cat_id] => 37
                            [cat_name] => 文艺
                            [parent_id] => 36
                            [son] => Array
                                (
                                    [0] => Array
                                        (
                                            [cat_id] => 39
                                            [cat_name] => 小说
                                            [parent_id] => 37
                                        )

                                    [1] => Array
                                        (
                                            [cat_id] => 40
                                            [cat_name] => 文学
                                            [parent_id] => 37
                                        )

                                )

                        )

                )

        )

    [2] => Array
        (
            [cat_id] => 43
            [cat_name] => 家用电器
            [parent_id] => 0
            [son] => Array
                (
                    [0] => Array
                        (
                            [cat_id] => 44
                            [cat_name] => 大家电/厨房家电
                            [parent_id] => 43
                            [son] => Array
                                (
                                    [0] => Array
                                        (
                                            [cat_id] => 45
                                            [cat_name] => 消毒柜
                                            [parent_id] => 44
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [cat_id] => 46
                            [cat_name] => 生活电器
                            [parent_id] => 43
                            [son] => Array
                                (
                                    [0] => Array
                                        (
                                            [cat_id] => 47
                                            [cat_name] => 电风扇
                                            [parent_id] => 46
                                        )

                                )

                        )

                )

        )

)


一个简单的无限级分类就完成了。

你可能感兴趣的:(无限级分类--1(商品分类))