Thinkphp 无限级分类
程序员文章站
2024-02-18 10:10:58
...
自已写的一个无限级分类,初级程序员,不足的地方,请指教!!
---------------------InfiniteModel.class.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
{
$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;
}
}
}
?>
--------------调用 Model--------
$InfiniteOb=new InfiniteModel(M('admin_node'),'id','pid','name','─');
返回的是一个结果数组
----------------效果图----------------AD:真正免费,域名+虚机+企业邮箱=0元