This example describes the PHP template engine smarty built-in function Foreach,foreachelse usage. Share to everyone for your reference, specific as follows:
In the Smarty template, you can use foreach to repeat a block. And in the template, you need to allocate an array from PHP. This array can be a multidimensional array. The {foreach} tags in Smarty are the same as foreach in PHP, but their one is used in the template file and one is used in PHP scripts. Therefore, the syntax is different. However, they all function the same, that is, traversing the contents of the array. There is also a {foreachelse} tag relative to the {foreach} tag, and the {Foreachelse} tag works by executing the contents of the tag if the array is empty. {foreach} and {/foreach} in the template must be in pairs, it has four parameters, where the From and item two parameters are necessary. For its parameters, see the following list:
Property |
type |
whether you must |
Default Value |
Description |
From |
String |
Yes |
N/A |
The name of the array to loop |
Item |
String |
Yes |
N/A |
Variable name of the current processing element |
Key |
String |
No |
N/A |
The key name of the currently processed 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 instance.
Example idea: Take out the content from the database, assign to an array variable $_html, assign the array variable to the template, and then iterate through the array in the template
Test.sql (SQL data used)
--
---table's structure ' user '-
-
CREATE table IF not EXISTS ' user ' (
' id ' mediumint (8) unsigned not NULL auto_incremen T,
' username ' varchar NOT null,
' email ' varchar NOT NULL,
' addtime ' datetime NOT NULL default ' 0000-0 0-00 00:00:00 ',
PRIMARY KEY (' id ')
) Engine=innodb DEFAULT Charset=utf8 auto_increment=7;
---The
data in the Dump 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 Road ', ' 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 Doll ', ' jghlw@sina.com ', ' 2011-10-24 00:00:00 ');
init.inc.php (template initialization file)
<?php
define (' Root_path ', DirName (__file__));//Set the 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 template directory
$_tpl->compile_dir = Root_path. '. /com/'; Re-specify the compilation directory
$_tpl->left_delimiter = ' <{';//re-assign left delimiter
$_tpl->right_delimiter = '}> ';//re-assign right delimiter
?>
index.php (main 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 ');//Connect to the database, please set your own
$_mysqli-> Set_charset (' UTF8 '); Set encoding
$_result = $_mysqli->query ("Select Username,email,addtime from User ID ASC");
$_html = Array ();
while (!! $_ROW=$_RESULT->FETCH_ASSOC ()) {
$_html[] = $_row;
}
$_tpl->assign (' data ', $_html); Assign the array to the template
$_tpl->display (' index.tpl ');//Introduce template
$_mysqli->close ();//Close database, release resources
?>
TPL/INDEX.TPL (template file for main file index.php)
Execution results:
Finally, in the main file index.php, passing past array $_html is a two-dimensional array. Keep Variables $smarty. Foreach is used based on the name attribute in the {foreach} tag, and the reserved variable attributes used are: first (the initial record), last (final record), iteration (always start with 1, 1 per execution), Total (used to show the number of loops performed)
More about PHP Interested readers can view the site topics: "Smarty Template Primer Tutorial", "PHP Template Technology Summary", "PHP based on PDO Operation Database Skills summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", " Introduction to PHP Basic Grammar, "Introduction to PHP object-oriented programming", "PHP string (String) Usage Summary", "Php+mysql Database Operations Tutorial" and "PHP common database Operation Skills Summary"
I hope this article will help you with the PHP program design based on Smarty template.