Home> php教程> php手册> body text

The difference between foreach and volist in ThinkPHP

巴扎黑
Release: 2016-12-07 14:43:06
Original
2258 people have browsed it

1.foreach tag
foreach tag is used for loop output:
foreach(name, item, key)
name (required): the data template variable to be output
item (required): loop single original variable
key (optional) : The key variable of the loop, the default value is key
Example:

{$vo.id}
{$vo.name}

The foreach tag is simpler than the volist tag and does not have as many functions as the volist tag. The advantage is that objects can be traversed and output, and the volist tag is usually used to output arrays.

2.volist tag
volist tag is mainly used to loop through the output of data sets or multi-dimensional arrays in templates
volist(name, id, offset, length, key, mod, empty)
name (required): the data model to be output Variable
id (required): loop variable
offset (optional): offset of the data to be output
length (optional): length of the output data
key (optional): key variable of the loop, the default value is i
mod (Optional): Modulo the key value, the default is 2 (used for even-numbered row queries, etc.)
empty (optional): The string displayed if the data is empty

Usually the result returned by the select method of the model is a two Dimensional arrays can be output directly using the volist tag.
First assign a value to the template in Action:
$user=M('User');
$list=$user->select();
$this->assign('list',$list);
$this->display();
The template is defined as follows, output the number and name

{$vo.id}
{$vo.name}

Supports outputting partial data, such as outputting the 5th-15th records:

{ $vo.id}
{$vo.name}

Output even-numbered row records:

{$vo.name}

The mod attribute is also used to control the line wrapping of certain records:

{$vo.name}


Output loop variable:

{$k}.{$vo.name}

If key is not specified For attributes, the loop variable i is used by default, for example:

{$i}.{$vo.name}

If you want to output the index of the array, you can use the key variable directly. Unlike the loop variable, the key is determined by the data itself, not controlled by the loop, for example:

{$key}.{$vo.name}

volist also has an alias iterate, and its usage is the same as volist.

Starting from version 2.1, functions are allowed to be used to set data sets, such as:

{$vo.name}


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
Latest Issues
Code has no effect