無線分類新寫法,是不是很吊@!

<?php
/**
 * ***************************************************************
 * 商品分類 2016年04月26日18:42:22
 * ***************************************************************
 * @param
 * @return void
 * @author 王迪<[email protected]>
 * @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 王迪<[email protected]>
     * @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!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章