Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
last index of an foreach loop

 
Post new topic   Reply to topic    Smarty Forum Index -> Help Wanted (commercial)
View previous topic :: View next topic  
Author Message
bhanu
Smarty n00b


Joined: 25 Jun 2010
Posts: 1

PostPosted: Fri Jun 25, 2010 2:26 pm    Post subject: last index of an foreach loop Reply with quote

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} {else}-{/if}
{/foreach}

i want that when their is last city name after this its come no - sign 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);
?>
Back to top
View user's profile Send private message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 420
Location: Kathmandu, Nepal

PostPosted: Tue Jun 29, 2010 9:34 am    Post subject: Try this Reply with quote

May be you wanted this:
Code:
India - USA - Japan - China


The template tags are:
Code:
{foreach from=$cityList key=myId item=i name=foo}
{$i.location_name}{if not $smarty.foreach.foo.last} - {/if}
{/foreach}


And, I guess, your PHP looks like:

Code:
$cityList = array(
   array(
      'location_name'=> 'India',
   ),
   array(
      'location_name'=> 'USA',
   ),
   array(
      'location_name'=> 'Japan',
   ),
   array(
      'location_name'=> 'China',
   ),
);
$smarty->assign('cityList', $cityList);
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Help Wanted (commercial) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP