1. 程式人生 > >【thinkphp】thinkphp遞迴迴圈欄目按照樹形結構無限極輸出

【thinkphp】thinkphp遞迴迴圈欄目按照樹形結構無限極輸出

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;

$newData[$v['catid']]=$v;

//echo $nstr.$v['catname']."<br />";

if($v['child']==1)//如果有子欄目

{

$newData=$newData+$this->categoryTree($v['catid'],$level);

}

}

}

return $newData;

}

php遞迴欄目儲存為陣列