thinkphp实现无级限栏目树形递归子栏目
private function categoryTree($parentid,$level) //因为是本类中使用所以定于为私有函数
{
$Category= D('Category');
$result = $Category->where("`parentid`=".$parentid)->order("listorder desc,catid desc")->select();
if($result)
{
$count=count($result);//当前子栏目个数
$level++;//子栏目层级
foreach($result as $v)
{
$index++;
if($count==$index) $step="└─";
else $step="├─";
$step.=str_repeat(' ',$level-1);
$nbsp=str_repeat(' ',$level-1);
$nstr=$nbsp.$step;
if($parentid==0) $nstr='';
$v['step']=$nstr;
$this->newData[$v['id']]=$v; //这一行是具体实际工作调试的成功效果,需要在类中建立属性 private $newData=array();
//目的就是将数据按查询的顺序存放到newData数组中
//$newData[$v['catid']]=$v; //如果直接用这个变量数据顺序不是希望的结果
if($v['child']==1)//如果有子栏目
{
$newData=$newData+$this->categoryTree($v['catid'],$level);
}
}
}
// return $newData;
}
php递归栏目保存为数组

浙公网安备 33010602011771号