Home  >  Article  >  php教程  >  PHP模板引擎Smarty内建函数foreach,foreachelse用法分析,smartyforeachelse

PHP模板引擎Smarty内建函数foreach,foreachelse用法分析,smartyforeachelse

WBOY
WBOYOriginal
2016-06-13 08:41:59864browse

PHP模板引擎Smarty内建函数foreach,foreachelse用法分析,smartyforeachelse

本文实例讲述了PHP模板引擎Smarty内建函数foreach,foreachelse用法。分享给大家供大家参考,具体如下:

在 Smarty 模板中,您可以使用 foreach 来重复一个区块。而在模板中则需要从 PHP 中分配过来一个数组。这个数组可以是多维数组。Smarty 中 {foreach} 标记和 PHP 中 foreach 相同,不同的是它们的一个在模板文件中使用,一个在 PHP 脚本中使用。因此,语法会不同。但是,它们的作用都是相同的,即遍历数组中的内容。与 {foreach} 标记相对的还有一个 {foreachelse} 标记,{foreachelse} 标记的作用是:如果数组为空,那么就执行该标记内的内容。 模板中 {foreach} 和 {/foreach} 必须是成对的出现,它有四个参数,其中, from 和 item 两个参数是必要的。关于它的参数请看下面列表:

属性 类型 是否必须 缺省值 描述
from string Yes n/a 待循环数组的名称
item string Yes n/a 当前处理元素的变量名称
key string No n/a

当前处理元素的键名

name string No n/a 该循环的名称,用于访问该循环

我们通过一个实例,来演示 Smarty 中 {foreach} 和 {foreachelse} 的使用。

实例思路:从数据库中取出内容,赋给一个数组变量 $_html ,再给这个数组变量分配给模板,然后在模板中进行该数组的遍历

test.sql (使用到的 SQL 数据)

--
-- 表的结构 `user`
--
CREATE TABLE IF NOT EXISTS `user` (
 `id` mediumint(8) unsigned NOT NULL auto_increment,
 `username` varchar(50) NOT NULL,
 `email` varchar(50) NOT NULL,
 `addTime` datetime NOT NULL default '0000-00-00 00:00:00',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
--
-- 转存表中的数据 `user`
--
INSERT INTO `user` (`id`, `username`, `email`, `addTime`) VALUES
(1, '苍井空', 'canjingkong@sina.com.cn', '2011-10-24 00:00:00'),
(2, '樱木花道', 'ymhd@163.com', '2011-10-24 00:00:00'),
(3, '赤木晴子', 'chimiqingzi@yahoo.com,cn', '2011-10-24 00:00:00'),
(4, '流川枫', 'lcfeng@sina.com', '0000-00-00 00:00:00'),
(5, '蜡笔小新', 'labixiaoxin@sina.com', '2011-10-24 00:00:00'),
(6, '金刚葫芦娃', 'jghlw@sina.com', '2011-10-24 00:00:00');

init.inc.php (模板初始化文件)

<&#63;php
 define('ROOT_PATH', dirname(__FILE__)); //设置网站根目录
 require ROOT_PATH.'/libs/Smarty.class.php'; //加载 Smarty 模板引擎
 $_tpl = new Smarty(); //创建一个实例对象
 $_tpl->template_dir = ROOT_PATH.'/tpl/'; //重新指定模板目录
 $_tpl->compile_dir = ROOT_PATH.'./com/'; //重新指定编译目录
 $_tpl->left_delimiter = '<{'; //重新指定左定界符
 $_tpl->right_delimiter = '}>'; //重新指定右定界符
&#63;>

index.php(主文件)

<&#63;php
 require 'init.inc.php'; //引入模板初始化文件
 global $_tpl;
 $_mysqli = new mysqli(); //创建一个 mysqli() 对象
 $_mysqli->connect('localhost','root','数据库密码','数据库名'); //连接数据库,请您自行设置
 $_mysqli->set_charset('utf8'); //设置编码
 $_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); //把数组分配到模板中
 $_tpl->display('index.tpl'); //引入模板
 $_mysqli->close(); //关闭数据库,释放资源
&#63;>

tpl/index.tpl(主文件 index.php 的模板文件)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>foreach,foreachelse</title>
</head>
<body>
 <table align="center" border="1" width="800">
  <{foreach from=$data item="row" name="ls"}> <!-- 这个foreach 循环分配过来的数组有几行数据 -->
   <!-- 在此,我们做几个保留变量 $smarty.foreach 的操作 -->
   <!-- 当数据显示第一条的时候,第一行的表格背景为黄色,使用属性:first -->
   <!-- 当数据显示最后一条的时候,最后一行的表格背景为蓝色,使用属性:last -->
   <!-- 显示下分配过来的数组的总个数,使用属性:total -->
   <{if $smarty.foreach.ls.first}>
   <tr bgcolor="#FFFF00"> <!-- 第一行背景为黄色 -->
   <{elseif $smarty.foreach.ls.last}>
   <tr bgcolor="#0000FF"> <!-- 最后一行背景为蓝色 -->
   <{else}>
   <tr>
   <{/if}>
    <td><{$smarty.foreach.ls.iteration}></td><!-- 注意:这里是保留变量 $smarty.foreach 的使用,iteration:总是从 1 开始,每执行一次增加 1 -->
    <{foreach from=$row item="col" name="lsin"}> <!-- 这个foreach 循环数组内的内容,显示在表格的<td></td>标签里 -->
     <td><{$col}></td>
    <{/foreach}>
   </tr>
  <{foreachelse}> <!-- 如果分配过来的数组中没有数据,那么就执行下面的操作! -->
   <tr>
    <td>对不起!暂时没有数据。</td>
   </tr>
  <{/foreach}>
  <tr>
   <td colspan="4" align="center">分配数组的总记录数为:<{$smarty.foreach.ls.total}>条</td>
  </tr>
 </table>
</body>
</html>

执行结果:

最后总结下,主文件 index.php 中传递过去的数组 $_html 为二维数组。保留变量 $smarty.foreach 的使用都是基于 {foreach} 标记中的 name 属性,使用到的保留变量属性有:first(首条记录)、last(末条记录)、iteration(总是从 1 开始,每执行一次增加 1)、total(用于显示循环执行的次数)

更多关于PHP相关内容感兴趣的读者可查看本站专题:《smarty模板入门基础教程》、《PHP模板技术总结》、《PHP基于pdo操作数据库技巧总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于smarty模板的PHP程序设计有所帮助。

您可能感兴趣的文章:

  • PHP模板引擎Smarty内建函数详解
  • PHP模板引擎Smarty内置变量调解器用法详解
  • PHP模板引擎Smarty自定义变量调解器用法
  • PHP模板引擎Smarty中的保留变量用法分析
  • PHP模板引擎Smarty之配置文件在模板变量中的使用方法示例
  • PHP模板引擎Smarty中变量的使用方法示例
  • smarty模板引擎从php中获取数据的方法
  • ThinkPHP使用smarty模板引擎的方法
  • 在PHP模板引擎smarty生成随机数的方法和math函数详解
  • PHP模板引擎Smarty的缓存使用总结
  • php smarty模板引擎的6个小技巧
  • [PHP]模板引擎Smarty深入浅出介绍
  • PHP模板引擎Smarty内建函数section,sectionelse用法详解
Statement:
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