Home > Article > Backend Development > thinkPHP implements recursive loop columns and outputs infinitely according to the tree structure, thinkphp recursion_PHP tutorial
This article describes the example of thinkPHP implementing a recursive loop column and outputs it infinitely according to the tree structure method. Share it with everyone for your reference, the details are as follows:
Here we use thinkphp recursive loop column to output infinitely according to the tree structure and save it as an array, which is convenient for template calling
The specific code is as follows:
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 recursive columns are saved as arrays
PS: The code in this article has not been formatted and beautified. Here are several formatting and beautification tools recommended on this site. I believe everyone can use it in future development:
php code online formatting and beautification tool:
http://tools.jb51.net/code/phpformat
JavaScript code beautification/compression/formatting/encryption tool:
http://tools.jb51.net/code/jscompress
Online XML formatting/compression tool:
http://tools.jb51.net/code/xmlformat
SQL code online formatting and beautification tool:
http://tools.jb51.net/code/sqlcodeformat
Readers who are interested in more thinkPHP-related content can check out the special topics on this site: "ThinkPHP Getting Started Tutorial", "ThinkPHP Common Methods Summary", "Smarty Template Basic Tutorial" and "PHP Template Technology Summary".
I hope this article will be helpful to everyone’s PHP programming based on the ThinkPHP framework.