Thinkphp 无限级分类

jerry thinkphp 2015年11月18日 收藏
自已写的一个无限级分类,初级程序员,不足的地方,请指教!!
  1. ---------------------InfiniteModel.class.php-------------
  2. <?php 
  3. /**
  4.  +++++++++++++++++++++++++++++++++++++++
  5.  *    无限级分类
  6.  +++++++++++++++++++++++++++++++++++++++
  7.  */
  8. class InfiniteModel 
  9. {
  10.         private $ClassObject;               //模型Model类名
  11.         private $ParentArr=array();           // 父级数组
  12.         private $InfiniteResult=array();   //存放无限级的数组
  13.         private $IdName;                    //记录的id字段名
  14.         private $FatherIdName;                //记录的父级id字段名
  15.         private $Name;                        //记录的名字
  16.         private $Fu;                        //分隔符  --或++ 自定义
  17.         /*
  18.          *   构造传值
  19.          */
  20.         public function __construct($Object,$IdName,$FatherIdName,$Name,$Fu)
  21.         {
  22.             $this->ClassObject=$Object;
  23.             $this->IdName=$IdName;
  24.             $this->FatherIdName=$FatherIdName;
  25.             $this->Name=$Name;
  26.             $this->Fu=$Fu;
  27.         }
  28.         /*
  29.          * 返回无限级数组
  30.          */
  31.         public function FinAllyResult()
  32.         {
  33.              $Result=$this->ClassObject->field("{$this->IdName},{$this->FatherIdName},{$this->Name}")->select(); 
  34.              foreach ($Result as $key =>$value )
  35.              {
  36.                     $this->InfiniteArr($value[$this->FatherIdName],$value[$this->Name],$value[$this->IdName]);
  37.              }
  38.             return $this->InfiniteResult;
  39.         }
  40.         //生成无限级数组
  41.         private function InfiniteArr($Fields,$name,$id)
  42.        {
  43.             if( empty($Fields)  )
  44.             {    
  45.                 $Middle=array($name=>$id);
  46.                 if(in_array($Middle,$this->ParentArr))
  47.                 {
  48.                     return false;
  49.                     break;
  50.                 }else{
  51.                     $this->ParentArr[]=$Middle;     //找到一个父级
  52.                     $keyarr=array_keys($Middle);
  53.                     $this->InfiniteResult[$Middle[$name]]=$keyarr[0];
  54.                     $multiple=$this->ClassObject->where("{$this->FatherIdName}={$Middle[$name]}")->field("{$this->IdName},{$this->Name}")->select(); 
  55.                     $this->recursion($multiple,1);
  56.                     return false;
  57.                     break;
  58.                 }
  59.             }else{
  60.                  $Result=$this->ClassObject->where("{$this->IdName}=".$Fields)->field("{$this->IdName},{$this->Name},{$this->FatherIdName}")->select();
  61.                  $this->InfiniteArr($Result[0][$this->FatherIdName],$Result[0][$this->Name],$Result[0][$this->IdName]);
  62.             }
  63.        }
  64.       //递归求数组
  65.        private function recursion( $sonarray,$count)
  66.        {
  67.                if( is_array($sonarray) && !empty($sonarray)  )
  68.                {
  69.                        $fu='';
  70.                        for( $i=0;$i<$count;$i++)
  71.                        {
  72.                            $fu.=$this->Fu;
  73.                        }
  74.                        $count++;
  75.                        foreach( $sonarray as $key => $value  )
  76.                        {
  77.                            $this->InfiniteResult[$value[$this->IdName]]=$fu.$value[$this->Name];
  78.                            $multiple=$this->ClassObject->where("{$this->FatherIdName}={$value[$this->IdName]}")->field("{$this->IdName},{$this->Name}")->select();
  79.                            $this->recursion($multiple,$count);
  80.                        }
  81.        
  82.                }else{
  83.                    return false;
  84.                    break;
  85.                }
  86.        }
  87. }
  88. ?>


  89. --------------调用 Model--------
  90. $InfiniteOb=new InfiniteModel(M('admin_node'),'id','pid','name','─');
  91. 返回的是一个结果数组
----------------效果图----------------