Home Backend Development PHP Tutorial Thinkphp的volist标签嵌套循环使用教程_php实例

Thinkphp的volist标签嵌套循环使用教程_php实例

May 17, 2016 am 08:39 AM
thinkphp Nested cycle

本文较为详细的对ThinkPHP的volist标签嵌套的用法阐述如下:

首先,在Thinkphp开发手册中,有关于标签嵌套的解释说明。如下:

标签嵌套:

模板引擎支持标签的多层嵌套功能,可以对标签库的标签指定可以嵌套。
系统内置的标签中,volist(及其别名iterate)、switch、if、elseif、else、foreach、compare(包括所有的比较标签)、(not)present、(not)empty、(not)defined等标签都可以嵌套使用。例如:

<volist name="list" id="vo">
<volist name="vo['sub']" id="sub">
{$sub.name}
</volist>
</volist>

Copy after login

上面的标签可以用于输出双重循环。

默认的嵌套层次是3级,所以嵌套层次不能超过3层,如果需要更多的层次可以指定TAG_NESTED_LEVEL配置参数。
但是在Action中具体应该怎样赋值“list”呢?从说明中可以看出,list应该是一个二维数组,下边是一段测试代码,经测试可以使用。

$Baojia=new Model('baojia');
$Class=new Model('class');
$parent=$Class->select();   
foreach($parent as $n=> $val){
$parent[$n]['voo']=$Baojia->where('belongto=\''.$val['name'].'\'')->select();
}
$this->assign('list',$parent);
<volist name="list" id="vo">
    {$vo.name}<BR>
<volist name="vo['voo']" id="sub">
 {$sub.name}
</volist><BR>
</volist>

Copy after login

数据库中定义了两个表,一个是报价表,一个是分类表,实现的功能是像树形菜单一样,显示分类,每个分类下边是各个型号的报价。

代码主要功能是:

1.首先创建模型:

$Baojia=new Model('baojia');
$Class=new Model('class');

Copy after login

2.然后查询分类中的数据,这一步非常重要,因为我们知道,数据库查询返回的是类似表格的二维形式的数据,当我们取出单条数据时,相当与读取每行数据。当调用时,thinkphp后台会自动读取每一行数据。

$parent=$Class->select(); 
Copy after login

将报价中的数据存入$parent中,其中$n是$parent数组的序号,也就相当于存入$parent中的数据表,每行添加一个索引,这个索引指向属于这个分类的报价。

foreach($parent as $n=> $val){
$parent[$n]['voo']=$Baojia->where('belongto=\''.$val['name'].'\'')->select();   
}
Copy after login

3.最后:

$this->assign('list',$parent);

Copy after login

显示输出!

通过这个程序,可以更深入的理解标签,其实如果在数据库操作中,标签的name只能assign 成数据库表类型(当然也可以是数组型,因为数据库查询得到的数据本身就是数组型的),当我们在视图页面调用标签时,特别是嵌套调用时,始终记住每一层的name都必须是数组型的,像本程序中,最外层,这里的list就是我们最初定义的$parent,这个变量指向查询class表得到的数据表,里层,也就是$parent[$n]['voo']所指向的数据表,也就是报价表中的相应数据。

通过这样的分析,条理已经很清晰了,举一反三就可以实现N重循环,当然如果需要更多的层次可以指定TAG_NESTED_LEVEL配置参数。

这样的话,就可以实现例如:国家->省->市->县->乡镇等多重循环

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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.

Can generic functions in Go be nested within each other? Can generic functions in Go be nested within each other? Apr 16, 2024 pm 12:09 PM

Nested Generic Functions Generic functions in Go 1.18 allow the creation of functions that apply to multiple types, and nested generic functions can create reusable code hierarchies: Generic functions can be nested within each other, creating a nested code reuse structure. By composing filters and mapping functions into a pipeline, you can create reusable type-safe pipelines. Nested generic functions provide a powerful tool for creating reusable, type-safe code, making your code more efficient and maintainable.

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.

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" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

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.

See all articles