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

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

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

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

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

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

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

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

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

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

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

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

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

Regarding PPT masking, many people must be unfamiliar with it. Most people do not understand it thoroughly when making PPT, but just make it up to make what they like. Therefore, many people do not know what PPT masking means, nor do they understand it. I know what this mask does, and I don’t even know that it can make the picture less monotonous. Friends who want to learn, come and learn, and add some PPT masks to your PPT pictures. Make it less monotonous. So, how to add a PPT mask? Please read below. 1. First we open PPT, select a blank picture, then right-click [Set Background Format] and select a solid color. 2. Click [Insert], word art, enter the word 3. Click [Insert], click [Shape]

Effects of C++ template specialization on function overloading and overriding Effects of C++ template specialization on function overloading and overriding Apr 20, 2024 am 09:09 AM

C++ template specializations affect function overloading and rewriting: Function overloading: Specialized versions can provide different implementations of a specific type, thus affecting the functions the compiler chooses to call. Function overriding: The specialized version in the derived class will override the template function in the base class, affecting the behavior of the derived class object when calling the function.

See all articles