thinkphp built label - cycling

Recycling label

1. foreach tag

Foreach tag usage and PHP syntax very close cycle of the output array or an attribute of an object, used as follows:

 

1) basic use - Data source template variables assigned by

New Index controllers, and the new index method to obtain data in a method and assigned to the template

 

foreach loop using output data templates index.html

 

Preview:

 

2) using the acquired data source function or method

Use acquire a data source

Use the index.html template function or method to get data source, in order to distinguish, for a query, but pay attention to the table to create a model

 

Preview:

 

Used to fetch data source

In public function file common.php file, the new getData function that returns the data.

 

Used to fetch data source index.html template, and outputs the cycle:

 

Preview:

 

2. volist label

volist tags typically query result set of data or two-dimensional array of output.

 

1) Basic use

In the Index controller, the new voshow method, assignment of template method

 

Be volist cycle through the template index.html

 

Preview:

 

2) function sets the data set

Can be used directly function setting data sets without requiring assignment variables passed to the template data set in the control variable, such as:

Using the function set in the setting data index.html template:

 

Index access method controller voshow preview:

 

3) the data output section - the offset and length attributes

Support part data output of the query results, such as the output of 5 to 15 records therein.

Using the offset and length attribute data in the output section of the template index.html

 

ID data in the database is not continuous. Preview:

 

note:

1. offset is offset from the beginning of the first few data; the number of data pieces outputted length, both functions similar to SQL statements limit.

4) Record the even - Property mod

In the index.html, the even recorded:

 

Preview:

 

mod property and for controlling the recording of a certain wrap, for example:

Use mod property controls line breaks in the index.html template.

 

Preview:

 

note:

1. mod is modulo property, i.e., take the remainder, in volist mod is the number (index) is divided by the row mod take the remainder, the number of rows from zero.

5) is empty data source system outputs --empty

在Index控制器中新建emshow方法,为index.html模板分配空数据

 

在index.html模板中,使用empty属性,输出为空提示

 

预览:

 

empty属性还支持变量输出:

在Index控制器的emshow方法中赋值给模板变量

 

在index.html模板中输出

 

预览:

 

6)输出循环变量——key

在index.html模板中输出循环变量:

 

访问voshow方法,预览:

 

注意:

1. 如果没有指定key属性的话,默认使用循环变量i。

2. 循环变量值从1开始。

如果要输出数组的索引,可以直接使用key变量,和循环变量不同的是,这个key是由数据本身决定,而不是循环控制的。

在index.html模板中,输出数组索引key:

 

预览:

 

3. for标签

 

在Index控制器中,新建forshow方法:

 

在index.html模板中循环

 

解析后的代码为:

 

预览:

 

测试for循环的其他属性

 

预览:

 

Guess you like

Origin www.cnblogs.com/mmzz3322/p/11121688.html