ThinkPHP implements recursive loop columns and outputs infinitus according to the tree structure _ php instance

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.