Implementation of nested loops of data by category in Thinkphp and nested in thinkphp
This article describes how to implement a nested loop of data in Thinkphp by category. 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:
Copy codeThe Code is as follows: <volist name = "list" id = "vo">
<Volist name = "vo ['sub']" id = "sub">
{$ Sub. name}
</Volist>
</Volist>
The tables I want to use here are: classification table (table 1) and data table (table 2)
The effect to be achieved is:
<Table 1_1>
<Table 2_1/>
<Table 2_2>
</Table 1_1>
<Table 1_2>
<Table 2_3/>
<Table 2_4>
</Table 1_2>
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:
Copy codeThe 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:
Copy codeThe Code is as follows: <volist name = "list" id = "vo">
<Li> <B >{$ vo. id} </B> </li>
<Volist name = "vo ['voo']" id = "sub">
<Li >{$ sub. title} </li>
</Volist>
</Volist>
Shows the effect:
I hope this article will help you with ThinkPHP programming.
Thinkphp nested loop
<Volist name = "article" id = "vo">
<Volist name = "vo ['Post _ tag']" id = "sub">
{$ Sub}
</Volist>
</Volist>
How to nest a loop in thinkphp