tags:

views:

385

answers:

3

How to get last index value of foreach loop in smarty,i m 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);
?>
A: 

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}

Lukman
A: 
{foreach from=$foo item=$bar name=foo}
    {if $smarty.foreach.foo.last}
        Total of({$smarty.foreach.foo.total}) Items <br />
        Data ({$bar})
    {/if}
{/foreach}
RobertPitt
+1  A: 

Your 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.

Bogdan Constantinescu
what is $smarty here can i use something else at that place like i use $main_smarty And in .php i assign $main_smarty->assign('cityList',$cityList);
Bhanu
As far as I know, `{$smarty}` is a reserved variable and you should use it. It shouldn't matter if your php object is named differently. You can find more about `{$smarty}` here: http://www.smarty.net/manual/en/language.variables.smarty.php
Bogdan Constantinescu
i used $smarty also but its not working
Bhanu
You tried with `$main_smarty` and also didn't work?
Bogdan Constantinescu
their is any other method to find it
Bhanu