zoukankan      html  css  js  c++  java
  • 无限分类树操作

    获取相应分类id的分类树:

    public static function getCategoryTree($id){
            //$model=M('category');
            if($id>0){            
                $obj=self::selectTable('category',array('id'=>$id),true);//$model->where(array('id'=>$id))->find();
                if(!is_null($obj)){
                    $childList=self::selectTable('category',array('parent_id'=>$id));//$model->where(array('parent_id'=>$id))->select();
                    if(!is_null($childList)){
                        foreach($childList as $val){
                            $obj['childList'][]=self::getCategoryTree($val['id']);
                        }
                    }
                }
                if(isset($obj['childList'])){
                    //二维数组排序
                    usort($obj['childList'],function($a,$b){
                        $subtractRes=$a['order']-$b['order'];
                        if($subtractRes<0){
                            return 1;
                        }elseif($subtractRes>0){
                            return -1;
                        }else{
                            return 0;
                        }
                    });
                }
                return $obj;
            }else{
                $rootList=self::selectTable('category',array('parent_id'=>$id));//$model->where(array('parent_id'=>$id))->select();
                if(!is_null($rootList)){
                    foreach($rootList as &$val){                    
                        $val=self::getCategoryTree($val['id']);
                    }
                }
                return $rootList;
            }
        }

    递归查找无限分类的父节点:

        //递归查找无限分类的父节点
        public static function getCategoryParent($categoryId){
            $category=self::selectTable('category',array('id'=>$categoryId),true);// M('category')->where(array('id'=>$categoryId))->find();
            if(!empty($category)){$category['parent']=self::getCategoryParent($category['parent_id']);
            }
            return $category;
        }

    从内存查询 表 以防止多次查库:

    //从内存查询 表 以防止多次查库
        private static function selectTable($tableName,array $where,$getFirst=false){
            $res=array();
            if(!isset(self::$tableData[$tableName])){
                self::$tableData[$tableName]=M($tableName)->select();
            }
            if(false===self::$tableData[$tableName]){
                return false;
            }
            
            is_null(self::$tableData[$tableName]) and self::$tableData[$tableName]=array();        
            foreach(self::$tableData[$tableName] as $val){
                $flag=true;
                foreach($where as $k=>$v){
                    if($val[$k]!=$v){
                        $flag=false;
                        break;
                    }
                }            
                $flag and $res[]=$val;
            }
            $getFirst and $res=current($res);
            empty($res) and $res=null;
            return $res;
        }
  • 相关阅读:
    NSURLSession的文件下载
    JSON解析(序列化和反序列化)
    NSURLSession的知识小记
    RunLoop的知识小记
    NSCach 的知识小记
    多图片下载综合案例-磁盘缓存处理
    模仿UIApplication创建单例
    LayoutSubviews的调用
    setValueForKeysWithDictionary的底层实现
    剑指offer 20:顺时针打印矩阵
  • 原文地址:https://www.cnblogs.com/zhudongchang/p/4646442.html
Copyright © 2011-2022 走看看