smarty 里面如何判断 是最后一条记录 就不输出 “|”

#1 helloniu

php代码
class city extends spController
{
        function index()
        {
         $sp= spClass("tbl_city");  
        $sql="select * from tbl_city";
        $this->rscity = $sp->findsql($sql);
        $this->display("tpl_city.html");
        }
}



模板文件
<{ foreach from=$rscity item=one }>  
<{$one.name}> |
<{ /foreach }>

smarty 里面如何判断 是最后一条记录    就不输出 “|”

2010-12-03 12:35:13

#2 helloniu

hoho  解决了  附代码

<{ foreach from=$rscity item=one key=key}>  
<{if $key neq 0}> | <{/if}>
<{$one.name}>
<{ /foreach }>

2010-12-03 12:51:49

#3 jake

这样不一定很好,因为key不一定是顺序的。

smarty有解决这个问题的方法:
<{ foreach from=$rscity item=one key=key name=abc}>  
<{if $smarty.foreach.abc.first eq 1}>我是循环第一个<{/if}>
<{if $smarty.foreach.abc.last eq 1}>我是循环最后一个<{/if}>
我是循环第<{$smarty.foreach.abc.iteration}>个
<{$one.name}>
<{ /foreach }>

foreach中可以有多个属性,第一个、最后一个、序号、双数、总数等,详见手册
http://www.smarty.net/docs/en/language.function.foreach.tpl

2010-12-03 13:53:21

#4 helloniu

:)      哈哈  学习了     谢谢斑竹了

2010-12-03 15:49:19

#5 helloniu

<{if not($smarty.foreach.citylist.last eq 1)}>|<{/if}>     smarty if语句里面还有not方法

2010-12-03 15:56:32

#6 jake

|     smarty if语句里面还有not方法
helloniu 发表于 2010-12-3 15:56
还有<{if $smarty.foreach.citylist.last neq 1}>|<{/if}>
或者
<{if ! $smarty.foreach.citylist.last)}>|<{/if}>
都是一样

2010-12-03 16:03:30