How to get last index value of foreach loop in smarty,im new for smarty I have used this code but its not working
{foreach from=$cityList key=myId item=i name=foo}
{$i.location_name}{if $main_smarty.foreach.foo.last}<hr>{else}-{/if}
{/foreach}
i want that when their is last city name after this its come horizontal line otherwise its like india-USA-Japan- but at last it come Japan-china
In .php i use
<?php
include_once('Smarty.class.php');
$main_smarty = new Smarty;
query to find citylist
$main_smarty->assign('cityList',$cityList);
?>
You're looking for this one:
{foreach from=$cityList key=myId item=i name=foo}
{if $smarty.foreach.foo.last}
<p>This is the last item from the array!</p>
{/if}
{/foreach}
As you see, the property you need to check is $smarty.foreach.foo.last
where foo
is the name of your object.
If $arr is the array you passed to the {foreach}, this will do the trick:
{$arr|@end}
In fact, it does not have to be called inside a {foreach}
I think newer versions of Smarty have a newer technique of solving this problem than what the other answers show. The example from the latest docs (at the time of writing) is using the @last
-property. You can use it like this: {if $item@last}...
Complete example:
{* Add horizontal rule at end of list *}
{foreach $items as $item}
<a href="#{$item.id}">{$item.name}</a>{if $item@last}<hr>{else},{/if}
{foreachelse}
... no items to loop ...
{/foreach}
{foreach from=$foo item=$bar name=foo}
{if $smarty.foreach.foo.last}
Total of({$smarty.foreach.foo.total}) Items <br />
Data ({$bar})
{/if}
{/foreach}
This actually worked for me:
{$myvar=$someArray@end}
{$myvar.someproperty}
My solution:
Array
(
[0] => car
[1] => toy
[2] => cat
[3] => gun
[4] => dog
)
{foreach from=$interest key=$key item=$item}
{$item.value}{if $key < ($interest|count - 1)}, {/if}
{/foreach}
My interest:
car, toy, cat, gun, dog
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.