Home > php教程 > php手册 > 将无限分类格式化为树形结构函数

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

WBOY
Release: 2016-06-07 11:38:38
Original
1260 people have browsed it

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

下拉框效果
将无限分类格式化为树形结构函数

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template