This article mainly introduces thinkPHP's method of implementing recursive loop columns and outputting infinitus in a tree structure, involving thinkPHP database queries, array traversal, string operations, and other techniques, for more information about how thinkPHP achieves recursive loops, see the example in this article. We will share this with you for your reference. The details are as follows:
The thinkphp recursive loop column is used to output an infinite number of outputs according to the tree structure and save it as an array, which facilitates template calling.
The Code is as follows:
Private function categoryTree ($ parentid, $ level) // This is a private function {$ Category = D ('category ') because it is used in this class '); $ result = $ Category-> where ("'parentid' = ". $ parentid)-> order ("listorder desc, catid desc")-> select (); if ($ result) {$ count = count ($ result ); // The current number of subtopics $ level ++; // subtopic level foreach ($ result as $ v) {$ index ++; if ($ count = $ index) $ step = "Your-"; else $ step = "Your-"; $ 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']."
"; If ($ v ['child '] = 1) // If a subtopic {$ newData = $ newData + $ this-> categoryTree ($ v ['catid'], $ level) ;}}return $ newData ;}
Save the php recursive column as an array
PS: the code in this article is not formatting and beautifying. We recommend several formatting and beautifying tools on this site. I believe you can use these tools in future development:
Php code online formatting and beautification tools:
Http://tools.php.net/code/phpformat
JavaScript code beautification/compression/formatting/encryption tools:
Http://tools.php.net/code/jscompress
Online XML formatting/compression tools:
Http://tools.php.net/code/xmlformat
SQL code online formatting and beautification tools:
Http://tools.php.net/code/sqlcodeformat