Thinkphp无限级分类

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

  


TAG标签:
版权声明:本文由美洲杯赌球发布于计算机教程,转载请注明出处:Thinkphp无限级分类