将无限分类格式化为树形结构函数

jerry thinkphp 2015年11月19日 收藏
将无限分类表数据输出为树形结构
/**
     * 功能 获取树形结构   注意:callback和level参数禁止传值
     * @param $model  string      表
     * @param $id     int         当前ID
     * @param $fields string    返回字段  前三个字段分别对应id,父id,名称  至少要包含前三个字段,返回的fullname是格式化的名称,如果表中本来有fullname字段建议做别名
     * @param $condition  max    查询条件  
     * @param $orderby  string    排序
     * @param $self boolean     是否包含当前ID数据
     * @param $onlyson  boolean    是否只返回下级
     * @param $return   string    返回数据类型,ids表示只返回id集合  tree返回树形结构数据
     * @return $datas array        返回的数据
     */
    protected function treeStructure($model='',$id=0,$fields='',$condition='',$orderby='',$self=true,$onlyson=false,$return='tree',$callback=false,$level=0){
                //验证参数类型
                if(!is_string($model) || !is_int($id) || !is_string($fields) || !(is_string($condition) || is_array($condition)) || !is_string($orderby) || !is_bool($self) || !is_bool($onlyson) || !in_array($return,array('ids','tree'))) return array();
        
                //验证参数值
                $fields_arr=explode(',',$fields);
        if(empty($model) || empty($fields) || count($fields_arr)<3)return array();
        
        //获取 id,父id,名称对应表中的字段
        foreach ($fields_arr as $k=>$f){
            $f=trim($f);
            $f_arr=explode('as',$f);
            $f_arr[0]=trim($f_arr[0]);
            $f_arr[1]=trim($f_arr[1]);
            $fields_arr[$k]=array('field'=>$f_arr[0],'alias'=>$f_arr[1]);
            if($k==2)break;
        }
        $field_id=$fields_arr[0]['field'];
        $field_pid=$fields_arr[1]['field'];
        $field_name=$fields_arr[2]['field'];
        
        
        $model=strtolower($model);
        
        //查询条件
        if(!empty($condition)){
            if(is_array($condition))$map=$map1=$map2=$condition;
            if(is_string($condition))$map['_string']=$map1['_string']=$map2['_string']=$condition;
        }
        
        
        $map[$field_pid]=$id;
        
        //查询
        if(empty($orderby)){
            $list=M()->table($model)->field($fields)->where($map)->select();
        }else{
            $list=M()->table($model)->field($fields)->where($map)->order($orderby)->select();
        }
        
        $datas=array();
        
        //临时字段变量
        $field_level=to_guid_string('level');
        $field_last=to_guid_string('last');
        $field_son_num=to_guid_string('son_num');
        $field_all_son_num=to_guid_string('all_son_num');
        
        if(!$callback && $self && $id!=0){
            $map1[$field_id]=$id;
            $info=M()->table($model)->field($fields)->where($map1)->find();
            $info[$field_level]=$level;
            $info[$field_last]=1;
            $info[$field_son_num]=count($list);
            $datas[]=$info;
            $level++;
        }
        
        if($onlyson){
            foreach ($list as $k=>$v){
                $v[$field_level]=$level;
                $v[$field_last]=(count($list)-1)==$k?1:0;
                $v[$field_son_num]=0;
                if($level==0)$info[$field_all_son_num]=0;
                $datas[]=$v;
            }
        }else{
            if(count($list)>0){
                foreach ($list as $k=>$v){
                    $v[$field_level]=$level;
                    $v[$field_last]=(count($list)-1)==$k?1:0;
                    $map2[$field_pid]=$v[$field_id];
                    $v[$field_son_num]=M()->table($model)->where($map2)->count();//获取下级数量
                    
                    $next_id=intval($v[$field_id]);
                    $next_level=$level+1;
                    $datasx=$this->treeStructure($model,$next_id,$fields,$condition,$orderby,true,false,$return,true,$next_level);
                    if($level==0){
                        $v[$field_all_son_num]=count($datasx);//获取所有子级数量
                    }
                    $datas[]=$v;
                    if(!empty($datasx)){
                        foreach ($datasx as $v1){
                            $datas[]=$v1;
                        }
                    }
                }
            }
        }
        
        if(!$callback && $self && $id!=0){
            $datas[0][$field_all_son_num]=count($datas)-1;
        }

        if(!$callback){
            $ids=array();
            $all_son_num=0;
            foreach ($datas as $k=>$v){
                $ids[]=$v[$field_id];
                
                if($v[$field_level]==0){
                    $all_son_num=$v[$field_all_son_num];
                    $i=0;
                    $v['fullname']=$v[$field_name];
                }else{
                    $i++;
                    $prev_num=$v[$field_level]-1;
                    if($prev_num>0){
                        $prev_icon_t=$i==$all_son_num?'└ ':'│ ';
                        $prev_icon=str_repeat('    '.$prev_icon_t,$prev_num);
                    }else{
                        $prev_icon='';
                    }
                    $icon=$v[$field_last] && $v[$field_son_num]==0?'    └ ':'    ├ ';
                    $v['fullname']=$prev_icon.$icon.$v[$field_name];
                }
                unset($v[$field_level],$v[$field_last],$v[$field_son_num]);
                if(isset($v[$field_all_son_num]))unset($v[$field_all_son_num]);
                $datas[$k]=$v;
            }
            
            if($return=='ids'){
                return $ids;
            }else{
                return $datas;
            }
        }else{
            return $datas;
        }
        
    }
列表效果


下拉框效果