In this paper, we describe the method of data nesting by classification in thinkphp. Share to everyone for your reference. The implementation methods are as follows:
When doing thinkphp, you use loops inside the loop, and the second loop is associated with the outside.
Thinkphp's official website gives the following documents:
Copy Code code as follows:
<volist name= "list" id= "Vo" >
<volist name= "vo[' Sub '" "Id=" sub ">
{$sub. Name}
</volist>
</volist>
The table I want to use here is: Category table (table 1), Datasheet (table 2)
The effect you want to implement 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), and then table 1 and table 2 associated with the query, and finally the two-dimensional array of output
The background code is as follows:
Copy Code code as follows:
$m =m (' table 1 ');
$m 1=m (' Table 2 ');
$parent = $m->select ();
foreach ($parent as $n => $val) {
$parent [$n] [' Voo ']= $m 1->where (' The field associated with table 2 and 1 = '. $val [' Table 1id ']. ') ->select ();
}
$this->assign (' list ', $parent);
$this->display ();
The foreground output shows:
Copy Code code 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>
The effect is shown in the following illustration:
I hope this article will help you with the thinkphp program design.