首页 > php教程 > php手册 > 正文

Thinkphp 无限级分类

WBOY
发布: 2016-06-07 11:44:36
原创
1269 人浏览过

自已写的一个无限级分类,初级程序员,不足的地方,请指教!!
---------------------InfiniteModel.class.php-------------<br> <?php <br /> /**<br>  +++++++++++++++++++++++++++++++++++++++<br>  *    无限级分类<br>  +++++++++++++++++++++++++++++++++++++++<br>  */<br> class InfiniteModel <br> {<br>         private $ClassObject;               //模型Model类名<br>         private $ParentArr=array();           // 父级数组<br>         private $InfiniteResult=array();   //存放无限级的数组<br>         private $IdName;                    //记录的id字段名<br>         private $FatherIdName;                //记录的父级id字段名<br>         private $Name;                        //记录的名字<br>         private $Fu;                        //分隔符  --或++ 自定义<br>         /*<br>          *   构造传值<br>          */<br>         public function __construct($Object,$IdName,$FatherIdName,$Name,$Fu)<br>         {<br>             $this->ClassObject=$Object;<br>             $this->IdName=$IdName;<br>             $this->FatherIdName=$FatherIdName;<br>             $this->Name=$Name;<br>             $this->Fu=$Fu;<br>         }<br>         /*<br>          * 返回无限级数组<br>          */<br>         public function FinAllyResult()<br>         {<br>              $Result=$this->ClassObject->field("{$this->IdName},{$this->FatherIdName},{$this->Name}")->select(); <br>              foreach ($Result as $key =>$value )<br>              {<br>                     $this->InfiniteArr($value[$this->FatherIdName],$value[$this->Name],$value[$this->IdName]);<br>              }<br>             return $this->InfiniteResult;<br>         }<br>         //生成无限级数组<br>         private function InfiniteArr($Fields,$name,$id)<br>        {<br>             if( empty($Fields)  )<br>             {    <br>                 $Middle=array($name=>$id);<br>                 if(in_array($Middle,$this->ParentArr))<br>                 {<br>                     return false;<br>                     break;<br>                 }else{<br>                     $this->ParentArr[]=$Middle;     //找到一个父级<br>                     $keyarr=array_keys($Middle);<br>                     $this->InfiniteResult[$Middle[$name]]=$keyarr[0];<br>                     $multiple=$this->ClassObject->where("{$this->FatherIdName}={$Middle[$name]}")->field("{$this->IdName},{$this->Name}")->select(); <br>                     $this->recursion($multiple,1);<br>                     return false;<br>                     break;<br>                 }<br>             }else{<br>                  $Result=$this->ClassObject->where("{$this->IdName}=".$Fields)->field("{$this->IdName},{$this->Name},{$this->FatherIdName}")->select();<br>                  $this->InfiniteArr($Result[0][$this->FatherIdName],$Result[0][$this->Name],$Result[0][$this->IdName]);<br>             }<br>        }<br>       //递归求数组<br>        private function recursion( $sonarray,$count)<br>        {<br>                if( is_array($sonarray) && !empty($sonarray)  )<br>                {<br>                        $fu='';<br>                        for( $i=0;$i                        {<br>                            $fu.=$this->Fu;<br>                        }<br>                        $count++;<br>                        foreach( $sonarray as $key => $value  )<br>                        {<br>                            $this->InfiniteResult[$value[$this->IdName]]=$fu.$value[$this->Name];<br>                            $multiple=$this->ClassObject->where("{$this->FatherIdName}={$value[$this->IdName]}")->field("{$this->IdName},{$this->Name}")->select();<br>                            $this->recursion($multiple,$count);<br>                        }<br>        <br>                }else{<br>                    return false;<br>                    break;<br>                }<br>        }<br> }<br> ?><br> <br> <br> --------------调用 Model--------<br> $InfiniteOb=new InfiniteModel(M('admin_node'),'id','pid','name','─');<br> 返回的是一个结果数组----------------效果图----------------
Thinkphp 无限级分类

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

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板