This article mainly introduces the usage of the Volist tag generated by the ThinkPHP template in a loop, and analyzes the Volist tag function, usage methods, and related precautions in detail based on the instance form, for more information about how to use the ThinkPHP template to generate a Volist tag, see the following example. We will share this with you for your reference. The details are as follows:
The volist tag is used to output a dataset or multi-dimensional array cyclically in the template.
Volist tag
In the module operation, the select () method returns a two-dimensional array, which can be output directly using volist:
User Name: {$ vo ['username']}
Email: {$ vo ['email ']}
Registration time: {$ vo ['regdate'] | date = "Y-m-d H: I ",###}
To output multi-dimensional arrays, see the ThinkPHP template Volist label nested loop output multi-dimensional array method.
Note: the name attribute value list (name = "list") cannot be changed at will. it must correspond to the template assignment instruction in the operation:
$this->assign( "list", $list );
Id indicates the cyclic variable, which can be specified at will, but cannot conflict with the name attribute.
Output part of data
If you want to output part of the data in the result set, you must specify the offset (data pointer) and length (number of data records) attributes.
Output 5th ~ 14 records:
User Name: {$ vo ['username']}
Email: {$ vo ['email ']}
Registration time: {$ vo ['regdate'] | date = "Y-m-d H: I ",###}
Output odd/even Records
The mod parameter in volist is equivalent to specifying a frequency. The system calculates the remainder (the % operator in PHP) of the mod parameter value in the current actual record. Combined with tags (such as eq tags), you can control the output data or data display format based on the frequency.
Example 1: output an even number of records:
User Name: {$ vo ['username']}
Email: {$ vo ['email ']}
Registration time: {$ vo ['regdate'] | date = "Y-m-d H: I ",###}
Example 2: output all records, but show different background colors in the table on the different rows:
Style = "background-color: # FFF ;">
I am a cell content |
I am also a cell content |
Tip: in actual use, you can flexibly set the mod parameter value, not only for parity.
Output loop variable
Specify the number of variables used by the key attribute to output the loop (not the primary key id of the data table ):
SN: {$ k}
User Name: {$ vo ['username']}
Email: {$ vo ['email ']}
Registration time: {$ vo ['regdate'] | date = "Y-m-d H: I ",###}
Output array index
Use the $ key variable directly to output the array index:
Array key: {$ key}
User Name: {$ vo ['username']}
Email: {$ vo ['email ']}
Registration time: {$ vo ['regdate'] | date = "Y-m-d H: I ",###}
Prompt
Different from the output loop variable, the key value depends on the data, rather than the volist loop output.