无线分类新写法,是不是很吊@!
<?php/*******************************************************************商品分类2016年04月26日18:42:22*****************************************************************@param*@returnvoid*@author王迪<2880707519@qq.com>*@license*@Copyright(c)2009-2016http://www.lvmaque.comAllrightsreserved.*/classModel_Shop_Class{protected$_table_name='shop_goods_class';protected$_primary_key='gc_id';/*******************************************************************获得排列好的分类信息2016年04月26日18:43:22*****************************************************************@param*@returnvoid*@author王迪<2880707519@qq.com>*@license*@Copyright(c)2009-2016http://www.lvmaque.comAllrightsreserved.*/publicstaticfunctiongetList(){$parent_0="selectgc_id,gc_parent_idfromsline_shop_goods_classwheregc_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;}//查找子集publicstaticfunctiongetChild(&$parent){//id的数组$idArray=Kohana_Util::only_array($parent['findIds'],'gc_id');//id连接城字符串$idString=implode(',',$idArray);$child="selectgc_id,gc_parent_idfromsline_shop_goods_classwheregc_parent_idin({$idString})";$childRes=DB::query(Kohana_Database::SELECT,$child)->execute()->as_array();if(count($childRes)<1){return;}$parent['findIds']=$childRes;//按parent_id把数组存储起来foreach($childResas$v){$parent[$v['gc_parent_id']][]=$v;}self::getChild($parent);}}
这么做查数据库的效率会比较高,是一层一层的往下查的
找子集的时候就从parent_id等于零开始查找,
然后循环0键值对应的数组,
再找里面的parent_id对应的键值是否存在,如果存在就把里面的继续遍历,如果不存在就结束递归!
O(∩_∩)O哈哈哈~!,我是不是很6,绝对原创,亲!转载只要998!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。