自已写的一个无限级分类,初级程序员,不足的地方,请指教!!---------------------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>
返回的是一个结果数组
----------------效果图----------------
AD:真正免费,域名+虚机+企业邮箱=0元