php学习交流博客

好好写代码,好好痛她

smarty section、foreach 学习笔记

section循环

section的运用了解:
1、循环一个简单的一维数组:
Example 7-30. Looping a simple array with {section}

 

1 <?php
2 $data = array(1000,1001,1002);
3 $smarty->assign('custid',$data);
4 ?>


//customer和下面的foo可以随便命名,作用其实仅仅是一个index下标,用来引用数组中的元素
1 {section name=customer loop=$custid}
2 id: {$custid[customer]}<br />
3 {/section}
4 <hr />

1 {section name=foo loop=$custid step=-1}
2 {$custid[foo]}<br />
3 {/section}




//输出
id: 1000<br />
id: 1001<br />
id: 1002<br />
<hr />
id: 1002<br />
id: 1001<br />
id: 1000<br />

2、不用assign数组直接在smarty中循环:
Example 7-31. {section} without an assigned array

//特别地设置了start,step属性用来控制循环
//$smarty.section.section的名字.index是一个特殊变量,用来显示当前循环的位置
1 {section name=foo start=10 loop=20 step=2}
2 {$smarty.section.foo.index}
3 {/section}
4 <hr />
5 {section name=bar loop=21 max=6 step=-2}
6 {$smarty.section.bar.index}
7 {/section}


//输出:
10 12 14 16 18
<hr />
20 18 16 14 12 10

3、section的name的值是随你定的
Example 7-32. Naming a {section}

1 {section name=anything loop=$myArray}
2 {$myArray[anything].foo}
3 {$name[anything]} //这种用法目前还没怎么用过,也不太清楚
4 {$address[anything].bar} //这种也是
5 {/section}


4、遍历一个关联数组,嵌套的数组

 1 <?php
 2 $data = array(
 3 array('name' => 'John Smith', 'home' => '555-555-5555',
 4 'cell' => '666-555-5555', 'email' => 'john@myexample.com'),
 5 array('name' => 'Jack Jones', 'home' => '777-555-5555',
 6 'cell' => '888-555-5555', 'email' => 'jack@myexample.com'),
 7 array('name' => 'Jane Munson', 'home' => '000-555-5555',
 8 'cell' => '123456', 'email' => 'jane@myexample.com')
 9 );
10 $smarty->assign('contacts',$data);
11 ?>


//section不用嵌套,因为只有一个数组,数组内部用$contacts[customer]得到
//每个数组,再用.键名来得到键值
1 {section name=customer loop=$contacts}
2 <p>
3 name: {$contacts[customer].name}<br />
4 home: {$contacts[customer].home}<br />
5 cell: {$contacts[customer].cell}<br />
6 e-mail: {$contacts[customer].email}
7 </p>
8 {/section}


The above example will output:

<p>
name: John Smith<br />
home: 555-555-5555<br />
cell: 666-555-5555<br />
e-mail: john@myexample.com
</p>
<p>
name: Jack Jones<br />
home phone: 777-555-5555<br />
cell phone: 888-555-5555<br />
e-mail: jack@myexample.com
</p>
<p>
name: Jane Munson<br />
home phone: 000-555-5555<br />
cell phone: 123456<br />
e-mail: jane@myexample.com
</p>
5、从数据库查询记录显示,实际上是显示二维数组,其实同上例一样
1 <?php
2 $sql = 'select id, name, home, cell, email from contacts '
3 ."where name like '$foo%' ";
4 $smarty->assign('contacts', $db->getAll($sql));
5 ?>


//结果:

 1 <table>
 2 <tr><th>&nbsp;</th><th>Name></th><th>Home</th><th>Cell</th><th>Email</th></tr>
 3 {section name=co loop=$contacts//第一维
 4 <tr>
 5 <td><a href="view.php?id={$contacts[co].id}">view<a></td> //第二维用.号来引用
 6 <td>{$contacts[co].name}</td>
 7 <td>{$contacts[co].home}</td>
 8 <td>{$contacts[co].cell}</td>
 9 <td>{$contacts[co].email}</td>
10 <tr>
11 {sectionelse}
12 <tr><td colspan="5">No items found</td></tr>
13 {/section}
14 </table>


6、嵌套的section

 

 

 1 <?php
 2 
 3 $id = array(1001,1002,1003);
 4 $smarty->assign('custid',$id);
 5 
 6 $fullnames = array('John Smith','Jack Jones','Jane Munson');
 7 $smarty->assign('name',$fullnames);
 8 
 9 $addr = array('253 N 45th', '417 Mulberry ln', '5605 apple st');
10 $smarty->assign('address',$addr);
11 
12 $types = array(
13 array'home phone', 'cell phone', 'e-mail'),
14 array'home phone', 'web'),
15 array'cell phone')
16 );
17 $smarty->assign('contact_type', $types);
18 
19 $info = array(
20 array('555-555-5555', '666-555-5555', 'john@myexample.com'),
21 array'123-456-4', 'www.example.com'),
22 array'0457878')
23 );
24 $smarty->assign('contact_info', $info);
25 
26 ?>

 


1 {section name=customer loop=$custid}
2 <hr>
3 id: {$custid[customer]}<br />
4 name: {$name[customer]}<br />
5 address: {$address[customer]}<br />
6 {section name=contact loop=$contact_type[customer]}
7 {$contact_type[customer][contact]}: {$contact_info[customer][contact]}<br />
8 {/section}
9 {/section}


The above example will output:

<hr>
id: 1000<br />
name: John Smith<br />
address: 253 N 45th<br />
home phone: 555-555-5555<br />
cell phone: 666-555-5555<br />
e-mail: john@myexample.com<br />
<hr>
id: 1001<br />
name: Jack Jones<br />
address: 417 Mulberry ln<br />
home phone: 123-456-4<br />
web: www.example.com<br />
<hr>
id: 1002<br />
name: Jane Munson<br />
address: 5605 apple st<br />
cell phone: 0457878<br />

 

smarty section 用法

1 {section loop = $varName[, start = $start, step = $step, max = $max, show = true]}
2 

name: section的名称,不用加$
$loop: 要循环的变量,在程序中要使用assign对这个变量进行操作。
$start: 开始循环的下标,循环下标默认由0开始
$step: 每次循环时下标的增数
$max: 最大循环下标
$show: boolean类型,决定是否对这个块进行显示,默认为true

这里有个名词需要说明:
循环下标:实际它的英文名称为index,是索引的意思,这里我将它译成"下标",主要是为了好理解。它表示在显示这个循环块时当

前的循环索引,默认从0开始,受$start的影响,如果将$start设为5,它也将从5开始计数,在模板设计部分我们使用过它,这是当前

{section}的一个属性,调用方式为Smarty.section.sectionName.index,这里的sectionName指的是函数原型中的name属性。
{section}块具有的属性值,分别为:
1. index: 上边我们介绍的"循环下标",默认为0
2. index_prev: 当前下标的前一个值,默认为-1
3. index_next: 当前下标的下一个值,默认为1
4. first: 是否为第一下循环
5. last: 是否为最后一个循环
6. iteration: 循环次数
7. rownum: 当前的行号,iteration的另一个别名
8. loop: 最后一个循环号,可用在section块后统计section的循环次数
9. total: 循环次数,可用在section块后统计循环次数
10. show: 在函数的声明中有它,用于判断section是否显示

 

*foreach循环

1. foreach:用于循环简单数组,它是一个选择性的section循环,它的定义格式为:

1 {foreach from=$array item=array_id}
2 {foreachelse}
3 {/foreach}

 

其中,from 指出要循环的数组变量,item为要循环的变量名称,循环次数由from所指定的数组变量的个数所决定。{foreachelse}用来当程序中传递过来的数组为空时的处理,下面是一个简单的例子:
===========================================
example6.tpl
===========================================

 

1 <html>
2 <head><title>这是一个foreach使用的例子</title></head>
3 <body>

 

这里将输出一个数组:<br

<{foreach from=$newsArray item=newsID}>
新闻编号:<{$newsID.newsID}><br>
新闻内容:<{$newsID.newsTitle}><br><hr>

<{foreachelse}>
对不起,数据库中没有新闻输出!
<{/foreach}>

posted on 2010-02-06 12:36 王吉刚 阅读(2418) 评论(0)  编辑 收藏 引用 网摘 所属分类: php学习转载区


只有注册用户登录后才能发表评论。
网站导航: