本文實例講述了thinkPHP實現遞歸循環欄目并按照樹形結構無限極輸出的方法。分享給大家供大家參考,具體如下:
這里使用thinkphp遞歸循環欄目按照樹形結構無限極輸出,并保存為一個數組,利于模板調用
具體代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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遞歸欄目保存為數組
希望本文所述對大家基于ThinkPHP框架的PHP程序設計有所幫助。