This article mainly introduces how to implement data nested loops by category in Thinkphp. it is applicable to nested loops with relevance and is a practical technique. For more information, see 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:
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}
I hope this article will help you with ThinkPHP programming.