View previous topic :: View next topic |
Author |
Message |
jmansa Smarty n00b
Joined: 13 Dec 2007 Posts: 4
|
Posted: Thu Dec 13, 2007 9:25 am Post subject: foreach only last record? |
|
|
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 |
|
master_kaos Smarty Regular
Joined: 02 Aug 2007 Posts: 54
|
Posted: Thu Dec 13, 2007 3:08 pm Post subject: |
|
|
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 |
|
Celeb Administrator
Joined: 17 Apr 2007 Posts: 1025 Location: Vienna
|
Posted: Thu Dec 13, 2007 4:23 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Dec 13, 2007 6:53 pm Post subject: |
|
|
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 |
|
jmansa Smarty n00b
Joined: 13 Dec 2007 Posts: 4
|
Posted: Thu Dec 13, 2007 7:22 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Dec 13, 2007 9:02 pm Post subject: |
|
|
{assign var=last value=$myarray|@end}
{$last.title}
{$last.author}
Or is your $posts array not a regular recordset? |
|
Back to top |
|
jmansa Smarty n00b
Joined: 13 Dec 2007 Posts: 4
|
Posted: Thu Dec 13, 2007 11:20 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat Dec 15, 2007 1:11 am Post subject: |
|
|
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 |
|
|