Thinkphp tutorial for using volist tag nesting loops

  • 2021-07-07 06:47:07
  • OfStack

In this paper, the usage of volist tag nesting in ThinkPHP is described as follows:

First of all, in the Thinkphp development manual, there are information about < volist > Explanation of label nesting. As follows:

Label nesting:

Template engine supports multi-layer nesting of tags, which can specify nesting of tags in tag library.
Among the built-in tags of the system, tags such as volist (and its alias iterate), switch, if, elseif, else, foreach, compare (including all comparison tags), (not) present, (not) empty and (not) defined can be nested. For example:


<volist name="list" id="vo">
<volist name="vo['sub']" id="sub">
{$sub.name}
</volist>
</volist>

The label above can be used to output a double loop.

The default nesting level is level 3, so the nesting level cannot exceed 3 levels. If more levels are needed, you can specify the TAG_NESTED_LEVEL configuration parameter.
But how should I assign "list" in Action? It can be seen from the description that list should be a 2-dimensional array, with a test code below, which can be used after testing.


$Baojia=new Model('baojia');
$Class=new Model('class');
$parent=$Class->select();   
foreach($parent as $n=> $val){
$parent[$n]['voo']=$Baojia->where('belongto=\''.$val['name'].'\'')->select();
}
$this->assign('list',$parent);
<volist name="list" id="vo">
    {$vo.name}<BR>
<volist name="vo['voo']" id="sub">
 {$sub.name}
</volist><BR>
</volist>

There are two tables defined in the database, one is quotation table and one is classification table. The function realized is like tree menu 1, showing classification, and the quotation of each model is below each classification.

The main functions of the code are:

1. Create the model first:


$Baojia=new Model('baojia');
$Class=new Model('class');

2. Then query the data in the classification. This step is very important because we know that the database query returns data in a 2-dimensional form like a table. When we take out a single piece of data, it is equivalent to reading each row of data. When the call < volist > thinkphp background will automatically read every 1 row of data.


$parent=$Class->select(); 

Save the data in the quote in $parent, where $n is the ordinal number of the $parent array, which is equivalent to the data table stored in $parent, and add one index to each row, which points to the quote belonging to this category.


foreach($parent as $n=> $val){
$parent[$n]['voo']=$Baojia->where('belongto=\''.$val['name'].'\'')->select();   
}

3. Finally:


$this->assign('list',$parent);

Show output!

Through this program, we can have a deeper understanding < volist > Tag, in fact, if you are in a database operation, < volist > Tag's name can only be assign into a database table type (of course, it can also be an array type, because the data obtained by database query itself is an array type), when we call in the view page < volist > Tags, especially when nested calls, always remember that every layer of name must be array type, like this program, outermost layer, < volist name="list" id="vo" > list here is the $parent we originally defined. This variable points to the data table obtained by querying the class table, and the inner layer < volist name="vo['voo']" id="sub" > That is, the data table pointed to by $parent [$n] ['voo'], which is the corresponding data in the quotation table.

Through this analysis, the organization has been very clear, and N re-loop can be realized by taking 1 against 3. Of course, if more levels are needed, TAG_NESTED_LEVEL configuration parameters can be specified.

In this way, we can achieve, for example, countries- > Province- > City- > County- > Multiple cycles such as villages and towns


Related articles: