This paper mainly introduces the usage of PHP template engine smarty built-in function Foreach,foreachelse, and analyzes the function of Foreach,foreachelse and the specific usage skill with instance form. specific as follows:
In the Smarty template, you can use foreach to repeat a chunk. In the template, you need to allocate an array from PHP. This array can be a multidimensional array. The {foreach} tag in Smarty is the same as foreach in PHP, but one of them is used in the template file and one in the PHP script. Therefore, the syntax will be different. However, they all have the same effect, that is, iterating through the contents of the array. There is also a {foreachelse} tag relative to the {foreach} tag, and the {Foreachelse} tag does this: If the array is empty, then the contents within that tag are executed. The template {foreach} and {/foreach} must be paired, it has four parameters, where the From and item two parameters are necessary. For its parameters, see the following list:
Properties |
type |
whether you must |
Default Value |
Description |
From |
String |
Yes |
N/A |
Name of the array to be looped |
Item |
String |
Yes |
N/A |
Variable name of the current processing element |
Key |
String |
No |
N/A |
The key name of the current processing element |
Name |
String |
No |
N/A |
The name of the loop that is used to access the loop |
We demonstrate the use of {foreach} and {foreachelse} in Smarty by an example.
Example idea: Take out the contents from the database, assign an array variable $_html, assign the array variable to the template, and then iterate over the array in the template.
Test.sql (SQL data used)
The structure of the----table ' user '--create table IF not EXISTS ' user ' (' ID ' mediumint (8) unsigned not NULL auto_increment, ' username ' Varch AR (+) is not null, ' email ' varchar (+) NOT null, ' Addtime ' datetime is NOT null default ' 0000-00-00 00:00:00 ', PRIMARY KEY (' ID ') Engine=innodb DEFAULT Charset=utf8 auto_increment=7;----Dump the data in the table ' user '--insert into ' user ' (' id ', ' username ', ' email ', ' Addtime ') VALUES (1, ' Cang jing empty ', ' canjingkong@sina.com.cn ', ' 2011-10-24 00:00:00 '), (2, ' Cherry blossom Trails ', ' ymhd@163.com ', ' 2011-10-24 00:00:00 '), (3, ' Red wood fine Son ', ' chimiqingzi@yahoo.com,cn ', ' 2011-10-24 00:00:00 '), (4, ' Chuan Feng ', ' lcfeng@sina.com ', ' 0000-00-00 00:00:00 '), (5, ' crayon Little new ', ' labixiaoxin@sina.com ', ' 2011-10-24 00:00:00 '), (6, ' King Kong Gourd ', ' jghlw@sina.com ', ' 2011-10-24 00:00:00 ');
init.inc.php (template initialization file)
<?php define (' Root_path ', DirName (FILE)); Set the Web site root directory require root_path. ' /libs/smarty.class.php '; Load Smarty template engine $_tpl = new Smarty (); Create an Instance object $_tpl->template_dir = Root_path. ' /tpl/'; Re-specify the template directory $_tpl->compile_dir = Root_path. /com/'; Re-specify the compilation directory $_tpl->left_delimiter = ' <{';//reassign left delimiter $_tpl->right_delimiter = '}> '; Re-specify right delimiter?>
index.php (Master file)
<?php require ' init.inc.php '; Introduction of template initialization file global $_tpl; $_mysqli = new mysqli (); Create a mysqli () object $_mysqli->connect (' localhost ', ' root ', ' Database password ', ' database name '); To connect to the database, please set $_mysqli->set_charset (' UTF8 ') yourself; Set Encoding $_result = $_mysqli->query ("Select Username,email,addtime from user order by ID ASC"); $_html = Array (); while (!! $_ROW=$_RESULT->FETCH_ASSOC ()) { $_html[] = $_row;} $_tpl->assign (' Data ', $_html);//assign array to template $_tpl-> Display (' Index.tpl '); Introduction of template $_mysqli->close (); Close the database and release the resources?>
TPL/INDEX.TPL (template file for Master file index.php)
Execution Result:
Finally, the $_html of the past array in the main file index.php is a two-dimensional array. The reserved variable $smarty. Foreach is used based on the name attribute in the {foreach} tag, with the reserved variable attribute used: First (top record), Last (end record), iteration (always starting from 1, incremented by 1 per execution), Total (used to show the number of times the loop was executed)