登录 用户中心() [退出] 后台管理 注册
   
您的位置: 首页 >> 程序员学前班[不再更新,只读] >> 主题: [php/模板]Smarty/foreach,foreachelse [可以嵌套的循环,较简单]     [回主站]     [分站链接]
标题
[php/模板]Smarty/foreach,foreachelse [可以嵌套的循环,较简单]
clq
浏览(0) + 2010-01-26 16:22:51 发表 编辑

关键字:

Smarty/foreach,foreachelse [可以嵌套的循环,较简单]

http://www.hbcms.com/main/smarty/language.function.foreach.html
--------------------------------------------------


clq
2010-1-26 16:24:17 发表 编辑

foreach,foreachelse

Table of Contents目录
iteration
first
last
show
total

Attribute Name Type Required Default Description
from string Yes n/a The name of the array you are looping through
item string Yes n/a The name of the variable that is the current element
key string No n/a The name of the variable that is the current key
name string No n/a The name of the foreach loop for accessing foreach properties

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

当前处理元素的键名

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

foreach loops are an alternative to section loops. foreach is used to loop over a single associative array. The syntax for foreach is much easier than section , but as a tradeoff it can only be used for a single array. foreach tags must be paired with /foreach tags. Required parameters are from and item . The name of the foreach loop can be anything you like, made up of letters, numbers and underscores. foreach loops can be nested, and the nested foreach names must be unique from each other. The from variable (usually an array of values) determines the number of times foreach will loop. foreachelse is executed when there are no values in the from variable.

foreach 是除 section 之外处理循环的另一种方案(根据不同需要选择不同的方案).
foreach 用于处理简单数组(数组中的元素的类型一致),它的格式比 section 简单许多,缺点是只能处理简单数组.
foreach 必须和 /foreach 成对使用,且必须指定 fromitem 属性.
name 属性可以任意指定(字母、数字和下划线的组合).
foreach 可以嵌套,但必须保证嵌套中的 foreach 名称唯一.
from 属性(通常是数组)决定循环的次数.
foreachelse 语句在 from 变量没有值的时候被执行.

Example 7-4. foreach
Smarty手册范例 7-4. foreach 演示

{* this example will print out all the values of the $custid array *}
{* 该例将输出数组 $custid 中的所有元素的值 *}
{foreach from=$custid item=curr_id}
id: {$curr_id}<br>
{/foreach}

输出结果:

id: 1000<br>
id: 1001<br>
id: 1002<br>

Example 7-5. foreach key
Smarty手册范例 7-5. foreach 键的演示

{* The key contains the key for each looped value

assignment looks like this:

$smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"),
array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")));
*}
{* 键就是数组的下标,请参看关于数组的解释 *}

{foreach name=outer item=contact from=$contacts}
{foreach key=key item=item from=$contact}
{$key}: {$item}<br>
{/foreach}
{/foreach}

输出结果:

phone: 1<br>
fax: 2<br>
cell: 3<br>
phone: 555-4444<br>
fax: 555-3333<br>
cell: 760-1234<br>

Foreach-loops also have their own variables that handle foreach properties. These are indicated like so: {$smarty.foreach.foreachname.varname} with foreachname being the name specified as the name attribute of foreach

foreach 循环有自己的变量名,使用该变量名可以访问该循环. 使用方法为{$smarty.foreach.foreachname.varname},其中 foreachname 即在 foreach 中指定的 name 属性.



总数:1 页次:1/1 首页 尾页  
总数:1 页次:1/1 首页 尾页  


所在合集/目录



发表评论:
文本/html模式切换 插入图片 文本/html模式切换


附件:



NEWBT官方QQ群1: 276678893
可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
验证问题说明申请入群原因即可.

Copyright © 2005-2020 clq, All Rights Reserved
版权所有
桂ICP备15002303号-1