Home > Backend Development > PHP Tutorial > Thinkphp's volist tag nested loop use

Thinkphp's volist tag nested loop use

不言
Release: 2023-03-30 09:50:01
Original
2088 people have browsed it

This article mainly introduces the method of ThinkPHP to implement volist tag nesting loop. Friends who need it can refer to it

This article explains the usage of ThinkPHP's volist tag nesting in more detail as follows:

First of all, in the Thinkphp development manual, there is an explanation about the nesting of tags. As follows:

Tag nesting:

The template engine supports the multi-level nesting function of tags, and you can specify whether the tags in the tag library can be nested.
Among the built-in tags in the system, volist (and its alias iterate), switch, if, elseif, else, foreach, compare (including all comparison tags), (not) present, (not) empty, (not) defined Tags can be nested. For example:

<volist name="list" id="vo">
<volist name="vo[&#39;sub&#39;]" id="sub">
{$sub.name}
</volist>
</volist>
Copy after login

The above tag can be used to output double loops.

The default nesting level is 3 levels, so the nesting level cannot exceed 3 levels. If you need more levels, you can specify the TAG_NESTED_LEVEL configuration parameter.
But how exactly should "list" be assigned a value in Action? As can be seen from the description, list should be a two-dimensional array. Below is a test code that can be used after testing.

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

Two tables are defined in the database, one is a quotation table and the other is a classification table. The function is to display the classification like a tree menu. Under each classification is the quotation of each model.

The main functions of the code are:

1. First create the model:

$Baojia=new Model(&#39;baojia&#39;);
$Class=new Model(&#39;class&#39;);
Copy after login

2. Then query the data in the classification. This step is very important, because we know that database query What is returned is data in a two-dimensional form similar to a table. When we take out a single piece of data, it is equivalent to reading each row of data. When calling , thinkphp background will automatically read each row of data.

$parent=$Class->select();
Copy after login

Store the data in the quotation into $parent, where $n is the serial number of the $parent array, which is equivalent to the data table stored in $parent. Each row adds an index, which points to Quotes that fall into this category.

foreach($parent as $n=> $val){
$parent[$n][&#39;voo&#39;]=$Baojia->where(&#39;belongto=\&#39;&#39;.$val[&#39;name&#39;].&#39;\&#39;&#39;)->select();   
}
Copy after login

3. Finally:

$this->assign(&#39;list&#39;,$parent);
Copy after login

Display the output!

Through this program, you can have a deeper understanding of the tag. In fact, during database operations, the name of the tag can only be assigned to a database table type (of course it can also be an array type, Because the data obtained by the database query itself is of array type), when we call the tag on the view page, especially when nested, always remember that the name of each layer must be of array type, like this In the program, the outermost layer, the list here is the $parent we initially defined. This variable points to the data table obtained by querying the class table. The inner layer , which is the data table pointed to by $parent[$n]['voo'], which is the corresponding data in the quotation table.

Through this analysis, the logic is very clear. N-fold loops can be achieved by drawing inferences from one example. Of course, if you need more levels, you can specify the TAG_NESTED_LEVEL configuration parameter.

In this case, multiple cycles such as country->province->city->county->township can be realized

Related recommendations:

How to use thinkPHP’s Html template tag

Example of the three-level linkage function of provinces and municipalities implemented by thinkPHP

ThinkPHP template judgment output Empty tag usage

The above is the detailed content of Thinkphp's volist tag nested loop use. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template