Home Backend Development PHP Tutorial ThinkPHP模板循环输出Volist标签用法实例详解_php实例

ThinkPHP模板循环输出Volist标签用法实例详解_php实例

Jun 07, 2016 pm 05:08 PM
thinkphp Loop output template

本文实例讲述了ThinkPHP模板循环输出Volist标签用法。分享给大家供大家参考,具体如下:

volist 标签用于在模板中循环输出数据集或者多维数组。

volist 标签

在模块操作中,select() 方法返回的是一个二维数组,可以用 volist 直接输出:

<volist name="list" id="vo">
用 户 名:{$vo['username']}<br />
电子邮件:{$vo['email']}<br />
注册时间:{$vo['regdate']|date="Y-m-d H:i",###}
</volist>

Copy after login

如果想输出多维数组,请参看《ThinkPHP模板Volist标签嵌套循环输出多维数组的方法》

注意:name的属性值 list (name="list")不可随意改变,需要与操作中的模板赋值指令对应:

$this->assign( "list", $list );

Copy after login

id 表示循环变量,可以随意指定,但不得与 name 属性冲突。

输出部分数据

如果要输出结果集中的部分数据,需要指定 offset(数据指针) 与 length(数据数据条数) 属性。

输出第 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>

Copy after login

输出奇/偶数记录

volist 中的 mod 参数相当于指定一个频率,系统会将当前的实际记录对 mod 参数值求余(PHP中的%运算符)运算。而配合判断标签(如eq标签),就可以按照频率控制输出的数据或数据显示的格式。

例子1,输出偶数记录:

<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>

Copy after login

例子2,输出所有记录,但让表格隔行显示不同的背景颜色的例子:

<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>

Copy after login

提示:实际使用中可以灵活设定 mod 参数的值,而不仅仅限于奇偶。

输出循环变量

指定 key 属性用于输出循环的变量次数(注意不是数据表主键 id):

<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>

Copy after login

输出数组索引

直接使用 $key 变量用于输出数组索引:

<volist name="list" id="vo">
数组key:{$key}<br />
用 户 名:{$vo['username']}<br />
电子邮件:{$vo['email']}<br />
注册时间:{$vo['regdate']|date="Y-m-d H:i",###}
<hr />
</volist>

Copy after login

提示

与输出循环变量不同的是,这个 key 值取决于数据本身,而不是 volist 循环输出决定的。

更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《smarty模板入门基础教程》及《PHP模板技术总结》。

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

How to run thinkphp project

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

There are several versions of thinkphp

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

How to run thinkphp

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

How to install thinkphp

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Which one is better, laravel or thinkphp?

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks

How to add PPT mask How to add PPT mask Mar 20, 2024 pm 12:28 PM

How to add PPT mask

How is the performance of thinkphp? How is the performance of thinkphp? Apr 09, 2024 pm 05:24 PM

How is the performance of thinkphp?

See all articles