无线分类新写法,是不是很吊@!

发布时间:2020-07-29 08:09:24 作者:woaiyiwa01
来源:网络 阅读:511
<?php
/**
 * ***************************************************************
 * 商品分类 2016年04月26日18:42:22
 * ***************************************************************
 * @param
 * @return void
 * @author 王迪<2880707519@qq.com>
 * @license
 * @Copyright (c) 2009-2016 http://www.lvmaque.com All rights reserved.
 */
class Model_Shop_Class{

    protected  $_table_name = 'shop_goods_class';
    protected  $_primary_key= 'gc_id';

    /**
     * ***************************************************************
     * 获得排列好的分类信息 2016年04月26日18:43:22
     * ***************************************************************
     * @param
     * @return void
     * @author 王迪<2880707519@qq.com>
     * @license
     * @Copyright (c) 2009-2016 http://www.lvmaque.com All rights reserved.
     */
    public static function getList(){
        $parent_0   =  "select gc_id,gc_parent_id from sline_shop_goods_class where gc_parent_id=0";
        $list       =  DB::query(Kohana_Database::SELECT,$parent_0)->execute()->as_array();
        $parentArray=array();
        $parentArray[0]=$list;
        $parentArray['findIds']=$list;

        self::getChild($parentArray);
        return $parentArray;
    }


    //查找子集
    public static function getChild(&$parent){
        //id的数组
        $idArray = Kohana_Util::only_array($parent['findIds'],'gc_id');

        //id连接城字符串
        $idString = implode(',',$idArray);

        $child = "select gc_id,gc_parent_id from sline_shop_goods_class where gc_parent_id in ({$idString})";
        $childRes = DB::query(Kohana_Database::SELECT,$child)->execute()->as_array();

        if(count($childRes)<1){
            return;
        }

        $parent['findIds']=$childRes;

        //按parent_id把数组存储起来
        foreach($childRes as $v){
            $parent[$v['gc_parent_id']][]=$v;
        }

        self::getChild($parent);
    }
}

这么做查数据库的效率会比较高,是一层一层的往下查的

找子集的时候就从parent_id等于零开始查找,

然后循环0键值对应的数组,

再找里面的parent_id对应的键值是否存在,如果存在就把里面的继续遍历,如果不存在就结束递归!

O(∩_∩)O哈哈哈~!,我是不是很6,绝对原创,亲!转载只要998!

推荐阅读:
  1. PHP中如何使用日历函数
  2. 利用Java与PHP进行Web开发有什么区别

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php model 无线分类

上一篇:C++--类与封装

下一篇:Linux基础命令---alias别名

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》