thinkphp volist tag Nesting loop use

Source: Internet
Author: User
This article mainly introduces the thinkphp implementation of Volist tag nested loop method, the need for friends can refer to the following

This article describes in more detail the use of thinkphp's volist tag nesting as follows:

First, in the thinkphp development manual, there are explanations for <volist> tag nesting. As follows:

Tag nesting:

The template engine supports multi-layered nesting of tags, and you can specify that tags can be nested on tag libraries.
In the system's built-in label, Volist (and its alias iterate), switch, if, ElseIf, else, foreach, compare (including all comparison labels), (not) present, (not) empty, (not) tags such as defined can be used in nested nesting. For example:

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

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

The default nesting level is level 3, so the nesting level cannot exceed 3 levels, and if more hierarchies are required, you can specify the Tag_nested_level configuration parameters.
But how exactly should the "list" be assigned in action? As can be seen from the description, list should be a two-dimensional array, below is a test code, can be used by 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>

The database defines two tables, one is the quote table, the other is the classification table, the function is like a tree menu, display classification, each category below is the price of each model.

The main functions of the code are:

1. Create the Model first:

$Baojia =new model (' Baojia '); $Class =new model (' Class ');

2. Then it is important to query the data in the classification, as we know that the database query returns data in two-dimensional form similar to the table, and when we take out a single piece of data, it is quite the same as reading each row of data. When <volist> is called, the thinkphp background automatically reads each row of data.

$parent = $Class->select ();

The data in the quote is stored in $parent, where $n is the ordinal of the $parent array, which is equivalent to the data table in the $parent, and each row adds an index that points to the quote belonging to the 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 get a deeper understanding of the <volist> tag, in fact, if the name of the,<volist> tag in the database operation can only assign into the database table type (of course, it can also be an array type, Because the database query gets the data itself is the array type, when we call the <volist> tag in the view page, especially when nested calls, always remember that each layer of name must be an array type, like this program, the outermost, <volist name= "list "Id=" Vo > here is our original definition of the $parent, this variable points to the Query class table to get the data table, the inner <volist name= "vo[' Voo ']" id= "sub", that is $parent[$ n][' Voo ') points to the data table, which is the corresponding data in the quote table.

Through this analysis, the organization is already clear, extrapolate can implement the N cycle, of course, if more levels are required to specify the Tag_nested_level configuration parameters.

In that case, you can do it. For example, the county------town---Township and so on multi-cycle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.