Thinkphp volist label nested loop tutorial, thinkphpvolist
The usage of volist tag nesting in ThinkPHP is described as follows:
First, the topic explains <volist> label nesting in the Thinkphp development manual. As follows:
Label nesting:
The template engine supports the multi-layer nesting function of tags. You can specify the tags of the tag library to be nested.
In the built-in labels, volist (and its alias iterate), switch, if, elseif, else, foreach, compare (including all comparison labels), (not) present, (not) empty, (not) defined, and other labels can be nested. For example:
<volist name="list" id="vo"><volist name="vo['sub']" id="sub">{$sub.name}</volist></volist>
The preceding labels can be used to output dual loops.
The default nesting level is 3, so the nesting level cannot exceed 3. If you need more layers, you can specify the TAG_NESTED_LEVEL configuration parameter.
But how should I assign a value to "list" in the Action? From the description, we can see that list should be a two-dimensional array. Below is a piece of test code, 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>
Two tables are defined in the database. One is a classification table and the other is a classification table. The function is to display a category like a tree menu. Each category is quoted for each model.
The main functions of the Code are:
1. Create a model first:
$Baojia=new Model('baojia');$Class=new Model('class');
2. this step is very important to query the data in the category, because we know that the database query returns two-dimensional data similar to the table. When we retrieve a single piece of data, it is equivalent to reading each row of data. When <volist> is called, thinkphp automatically reads each row of data in the background.
$parent=$Class->select();
Store the quoted data to $ parent, where $ n is the serial number of the $ parent array, which is equivalent to storing the data table in $ parent. An index is added to each row, this index points to the quotation for 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, you can have a deeper understanding of the <volist> label. In fact, if you are operating in a database, <volist> the tag name can only be assign as the database table type (of course, it can also be array type, because the data obtained by the database query itself is array type ), when we call the <volist> label on the view page, especially for nested calls, we always remember that the name of each layer must be array-type, such as the outermost layer in this program, <volist name = "list" id = "vo"> the list here is the $ parent we initially defined. This variable points to the data table obtained by querying the class table, <volist name = "vo ['voo']" id = "sub">, that is, the data table pointed to by $ parent [$ n] ['voo, that is, the corresponding data in the tables Table.
Through this analysis, the Organization is clear, and N re-loops can be achieved through the first step. Of course, if you need more layers, you can specify the TAG_NESTED_LEVEL configuration parameter.
In this way, multiple cycles such as country> province> city> County> township can be achieved.
In thinkphp, I made a three-layer nesting of volist. There is a form in the three-layer, and an error occurs when I put the input tag in the form on the third layer.
Can I change it to $ c ['child?
Nested volist in ThinkPHP
$ This-> assign ('list1', $ array );
<Volist name = "list1" id = "vo1">
<Volist name = "vo1 ['name']" id = "VO 2>
*****
</Volist>
</Volist>