码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-04-27 07:08:10      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:无线分类   php   model   

<?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!

本文出自 “好好学习” 博客,请务必保留此出处http://westds.blog.51cto.com/9850609/1768026

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

标签:无线分类   php   model   

原文地址:http://westds.blog.51cto.com/9850609/1768026

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