Implementation of nested loops of data in Thinkphp _ php instance

Source: Internet
Author: User
This article mainly introduces how to implement the nested loop of data in Thinkphp by category. it is a practical technique applicable to the Association of nested loops, for more information about how to implement nested loops of data in Thinkphp, see the examples in this article. Share it with you for your reference. The specific implementation method is as follows:

During thinkphp, nested loops are used in the loop, and the second loop is associated with the outside.
The thinkphp official website provides the following documents:

The code is as follows:



{$ Sub. name}

The tables I want to use here are: Classification Table (Table 1) and data table (Table 2)
The effect to be achieved is:

<表1_1>
<表2_1 />
<表2_2>

<表1_2>
<表2_3 />
<表2_4>

In fact, the principle is to first identify the classification table (Table 1), then associate table 1 and Table 2 for query, and finally output the data in a two-dimensional array.

The background code is as follows:

The code is as follows:

$ M = M ('Table 1 ');
$ M1 = M ('Table 2 ');

$ Parent = $ m-> select ();
Foreach ($ parent as $ n => $ val ){
$ Parent [$ n] ['voo'] = $ m1-> where ('field associated with Table 1 in table 2 = '. $ val ['table 1id']. '')-> select ();
}
$ This-> assign ('list', $ parent );
$ This-> display ();


Foreground output:

The code is as follows:


  • {$ Vo. id}


  • {$ Sub. title}



  • Shows the effect:

    I hope this article will help you with ThinkPHP programming.

    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.