Home >Backend Development >PHP Tutorial >ThinkPHP template loop output Volist tag usage example detailed explanation, thinkphpvolist_PHP tutorial
This article describes the usage of Volist tags in ThinkPHP template loop output. Share it with everyone for your reference, the details are as follows:
Thevolist tag is used to loop through output data sets or multi-dimensional arrays in templates.
volist tag
In module operation, the select() method returns a two-dimensional array, which can be output directly using volist:
<volist name="list" id="vo"> 用 户 名:{$vo['username']}<br /> 电子邮件:{$vo['email']}<br /> 注册时间:{$vo['regdate']|date="Y-m-d H:i",###} </volist>
If you want to output a multi-dimensional array, please refer to "ThinkPHP template Volist tag nested loop output multi-dimensional array method"
Note: The attribute value list of name (name="list") cannot be changed at will and needs to correspond to the template assignment instruction in the operation:
$this->assign( "list", $list );
id represents a loop variable, which can be specified at will, but must not conflict with the name attribute.
Output some data
If you want to output part of the data in the result set, you need to specify the offset (data pointer) and length (number of data items) attributes.
Output records 5~14:
<volist name="list" id="vo" offset="5" length='10'> 用 户 名:{$vo['username']}<br /> 电子邮件:{$vo['email']}<br /> 注册时间:{$vo['regdate']|date="Y-m-d H:i",###} <hr /> </volist>
Output odd/even records
The mod parameter in volist is equivalent to specifying a frequency, and the system will calculate the remainder (% operator in PHP) of the mod parameter value based on the current actual record. With the judgment tag (such as eq tag), the output data or data display format can be controlled according to the frequency.
Example 1, output even-numbered records:
<volist name="list" id="vo" mod="2"> <eq name="mod" value="0"> 用 户 名:{$vo['username']}<br /> 电子邮件:{$vo['email']}<br /> 注册时间:{$vo['regdate']|date="Y-m-d H:i",###} <hr /> </eq> </volist>
Example 2, output all records, but let the table display different background colors in alternate rows:
<table> <volist name="list" id="vo" mod="2"> <tr<eq name="mod" value="0"> style="background-color:#FFF;"</eq>> <td>我是单元格内容</td> <td>我也是单元格内容</td> </tr> </volist> </table>
Tip: In actual use, the value of the mod parameter can be set flexibly, not just odd and even.
Output loop variable
Specify the key attribute to output the variable number of loops (note that it is not the primary key id of the data table):
<volist name="list" id="vo" key="k"> 序 号:{$k}<br /> 用 户 名:{$vo['username']}<br /> 电子邮件:{$vo['email']}<br /> 注册时间:{$vo['regdate']|date="Y-m-d H:i",###} <hr /> </volist>
Output array index
Use the $key variable directly to output the array index:
<volist name="list" id="vo"> 数组key:{$key}<br /> 用 户 名:{$vo['username']}<br /> 电子邮件:{$vo['email']}<br /> 注册时间:{$vo['regdate']|date="Y-m-d H:i",###} <hr /> </volist>
Tips
Different from the output loop variable, this key value depends on the data itself, rather than the volist loop output.
Readers who are interested in more thinkPHP-related content can check out the special topics on this site: "ThinkPHP Getting Started Tutorial", "ThinkPHP Common Methods Summary", "Smarty Template Basic Tutorial" and "PHP Template Technology Summary".
I hope this article will be helpful to everyone’s PHP programming based on the ThinkPHP framework.