Home  >  Article  >  Backend Development  >  Thinkphp's volist tag nested loop usage tutorial, thinkphpvolist_PHP tutorial

Thinkphp's volist tag nested loop usage tutorial, thinkphpvolist_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:23:231164browse

Tutorial on using Thinkphp’s volist tag nested loop, thinkphpvolist

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

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

Tag nesting:

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

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

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('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>

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('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 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 3da5ee660f26a047cce0b9503e43e613, thinkphp background will automatically read each row of data.

$parent=$Class->select(); 

Save the data in the quotation into $parent, where $n is the serial number of the $parent array, which is equivalent to storing the data table in $parent. Each row adds an index, which points to the data belonging to this category. Quotation.

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, you can have a deeper understanding of the 3da5ee660f26a047cce0b9503e43e613 tag. In fact, during database operations, the name of the 3da5ee660f26a047cce0b9503e43e613 tag can only be assigned to a database table type (of course it can also be an array type, because database query The obtained data itself is of array type). When we call the 3da5ee660f26a047cce0b9503e43e613 tag on the view page, especially when nested, always remember that the name of each layer must be of array type. Like in this program, The outermost layer, 2c7953a8f6f793cfefe6eaa0b0257de7 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 d39b0f14e35afb6f48caa7dcc7922c4d, 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 realized by drawing inferences from one example. Of course, if you need more levels, you can specify the TAG_NESTED_LEVEL configuration parameter.

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

In thinkphp, I made a three-layer nesting of volist. There are forms in these three layers. When I put the input tag in the form in the third layer, an error occurred

Can I change it to $c['childid']?

Problems with volist nesting in ThinkPHP

$this->assign('list1',$array);
e6a67652dd96ed6d76d4bf750cf32c6d
f2e6da5c594640207d812e6d3d55a07e
*****
0c2cdd09f5f1098aaa1356781bc5fc07
0c2cdd09f5f1098aaa1356781bc5fc07

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/840750.htmlTechArticleThinkphp's volist tag nesting loop usage tutorial, thinkphpvolist This article provides a more detailed usage of ThinkPHP's volist tag nesting The explanation is as follows: First of all, in the Thinkphp development manual, there is...
Statement:
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