本文實例講述了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遞歸欄目保存為數組