Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

foreach only last record?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
jmansa
Smarty n00b


Joined: 13 Dec 2007
Posts: 4

PostPosted: Thu Dec 13, 2007 9:25 am    Post subject: foreach only last record? Reply with quote

I was wandering if it was possible to only show the latest record using foreach?
Right now my code looks like this and gets all the records:
Code:
{foreach from=$posts.title item=title key=i name="posts"}

Hope somebody can help!
Back to top
View user's profile Send private message
master_kaos
Smarty Regular


Joined: 02 Aug 2007
Posts: 54

PostPosted: Thu Dec 13, 2007 3:08 pm    Post subject: Reply with quote

yup, can do


{foreach from=$posts.title item=title key=i name="posts"}
{if $smarty.foreach.posts.last}
Display record
{/if}
{/foreach}
Back to top
View user's profile Send private message
Celeb
Administrator


Joined: 17 Apr 2007
Posts: 1025
Location: Vienna

PostPosted: Thu Dec 13, 2007 4:23 pm    Post subject: Reply with quote

You might wanna consider to only assign this last item to Smarty in the first place. It doesn't make very much sense to iterate a whole array just to output only the last item, does it?
_________________
Darn computers always do what I tell them to instead of what I want them to do.
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Dec 13, 2007 6:53 pm    Post subject: Reply with quote

Maybe consider the PHP array functions to manipulate the array pointer (since they can be used as modifiers):
Code:
{$myarray|@end}
{$myarray|@key}

The first statement moves the pointer to the end of the array and returns its value. The second statement returns the key for the current element -- which is the last element due to the previous statement.
Back to top
View user's profile Send private message
jmansa
Smarty n00b


Joined: 13 Dec 2007
Posts: 4

PostPosted: Thu Dec 13, 2007 7:22 pm    Post subject: Reply with quote

master_kaos wrote:
{foreach from=$posts.title item=title key=i name="posts"}
{if $smarty.foreach.posts.last}
Display record
{/if}
{/foreach}

This works like a charm, allthough... Is it possible to get another record into the foreach!

Now I'm only getting $title in the loop, but I also want to get the $author for each record... How do I go about that?
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Dec 13, 2007 9:02 pm    Post subject: Reply with quote

{assign var=last value=$myarray|@end}
{$last.title}
{$last.author}

Or is your $posts array not a regular recordset?
Back to top
View user's profile Send private message
jmansa
Smarty n00b


Joined: 13 Dec 2007
Posts: 4

PostPosted: Thu Dec 13, 2007 11:20 pm    Post subject: Reply with quote

boots wrote:
{assign var=last value=$myarray|@end}
{$last.title}
{$last.author}

I'm faily new at using smarty, so I can not figure out where to insert the example you are showing... Is it instead of my {if}?
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sat Dec 15, 2007 1:11 am    Post subject: Reply with quote

It replaces the entire thing -- looping a foreach and testing for the last record is not required. My example just forces the array to point at the last array element and then simply use that as your record of interest.

There may be a confusion, though: Do your attributes each have their own arrays? eg: a separate array for title, author, etc or is record packed into a single indexed array like a recordset (I assume the latter in my example).
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General 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