View previous topic :: View next topic |
Author |
Message |
m477 Smarty n00b
Joined: 09 Aug 2006 Posts: 3
|
Posted: Wed Aug 09, 2006 1:45 pm Post subject: Foreach loops help |
|
|
Hey there, I'm relatively new to smarty and I am updating a website that uses the smarty template engine, however I did not create the website. Currently there is a foreach loop that outputs every single activity recorded by a user. Once the user has had a fair ammount of activity the list starts to get fairly long, so I want to cut it down to say the last 10 activities.
The current code being used is:
Code: |
{foreach from=$USER_POINTS_OBJ->points_history item=history}
{$history.DateAdded}
{$history.cName}
{$history.rName}
{$history.Comments}
{$history.Points}
{/foreach}
|
I looked through the documentation on the smarty site for a variation, and I know in php you can just have a for loop, but there doesnt appear to be one with smarty. Can anyone help me out here?
Thanks. |
|
Back to top |
|
flourishing Smarty Rookie
Joined: 15 Oct 2005 Posts: 6
|
Posted: Wed Aug 09, 2006 3:30 pm Post subject: |
|
|
{section name=activity loop=USER_POINTS_OBJ->points_history max=10}
USER_POINTS_OBJ->points_history.DateAdded[activity]
USER_POINTS_OBJ->points_history.cName[activity]
USER_POINTS_OBJ->points_history.rName[activity]
USER_POINTS_OBJ->points_history.Comments[activity]
USER_POINTS_OBJ->points_history.Points[activity]
{/section}
maybe this can help you , tried it .
i haven't test it . |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Aug 09, 2006 5:37 pm Post subject: |
|
|
You can put max=N on the foreach loop to stop it short, but ideally you pass the template only the set of items you want displayed. |
|
Back to top |
|
m477 Smarty n00b
Joined: 09 Aug 2006 Posts: 3
|
Posted: Thu Aug 10, 2006 6:50 am Post subject: |
|
|
Code: |
{foreach from=$USER_POINTS_OBJ->points_history item=history max=10}
{$history.DateAdded}
{$history.cName}
{$history.rName}
{$history.Comments}
{$history.Points}
{/foreach}
|
I tried that and it didn't seem to do anything, still printed out 100's of lines. I also tried the section code that flourishing suggestion which caused the page to not load. Would it be helpfull if I posted the code thats calling all the data from the database? |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Aug 10, 2006 8:03 am Post subject: |
|
|
there is not attribute "max" in {foreach}.
do the "10 latest activities"-logic in php or mabye use a modifier like:
{foreach from=$USER_POINTS_OBJ->points_history|@array_slice:-10:10 item=history} |
|
Back to top |
|
m477 Smarty n00b
Joined: 09 Aug 2006 Posts: 3
|
Posted: Thu Aug 10, 2006 8:33 am Post subject: |
|
|
messju, what you described worked, however it shows the first 10 records of the points history, not the last 10.
Each time a user does an activity, it is stored in a table called PointsHistory, and there is a cell called ID which auto increments by one after every action. |
|
Back to top |
|
shannera Administrator
Joined: 13 Feb 2006 Posts: 802 Location: Edertal, Germany
|
Posted: Thu Aug 10, 2006 12:12 pm Post subject: |
|
|
As messju told numerous times, thats the lone problem of the business logic in the php script, in this case most likely the database query:
Code: |
SELECT * FROM PointsHistory ORDER BY ID DESC LIMIT 10
|
(note: mySQL syntax used) |
|
Back to top |
|
Watchopolis Smarty Rookie
Joined: 06 Aug 2014 Posts: 5
|
Posted: Tue Aug 26, 2014 10:55 pm Post subject: |
|
|
messju wrote: | there is not attribute "max" in {foreach}.
do the "10 latest activities"-logic in php or mabye use a modifier like:
{foreach from=$USER_POINTS_OBJ->points_history|@array_slice:-10:10 item=history} |
old but i hope you are still arround..
where is the documentation for @array_slice etc ? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Aug 27, 2014 9:01 am Post subject: |
|
|
what mohrt says. |
|
Back to top |
|
Watchopolis Smarty Rookie
Joined: 06 Aug 2014 Posts: 5
|
Posted: Wed Aug 27, 2014 10:15 am Post subject: |
|
|
messju wrote: | what mohrt says. |
does this means we can use php fucntion inside tpl files without using {php} {/php} ? |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Aug 27, 2014 5:55 pm Post subject: |
|
|
Yes.
PHP functions can be used from within templates as modifier or normal fuction.
So something like
Code: | {php_function($foo)} |
would work |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Aug 27, 2014 6:53 pm Post subject: |
|
|
Watchopolis wrote: | messju wrote: | what mohrt says. |
does this means we can use php fucntion inside tpl files without using {php} {/php} ? |
You can use PHP functions directly as modifiers, given that the first parameter can take the input. If that is not the case, you must wrap it in a modifier plugin to order the parameters appropriately.
In Smarty 3 you can use functions directly, ie. {foo()}, however there is a fine line here, as it may be tempting to use templates for logic they are not intended for, which is the same problem with {php} tags. Often it is better/cleaner to move logic to a plugin function or do PHP side and assign the results. |
|
Back to top |
|
Watchopolis Smarty Rookie
Joined: 06 Aug 2014 Posts: 5
|
Posted: Thu Aug 28, 2014 2:16 pm Post subject: |
|
|
mohrt wrote: | Watchopolis wrote: | messju wrote: | what mohrt says. |
does this means we can use php fucntion inside tpl files without using {php} {/php} ? |
You can use PHP functions directly as modifiers, given that the first parameter can take the input. If that is not the case, you must wrap it in a modifier plugin to order the parameters appropriately.
In Smarty 3 you can use functions directly, ie. {foo()}, however there is a fine line here, as it may be tempting to use templates for logic they are not intended for, which is the same problem with {php} tags. Often it is better/cleaner to move logic to a plugin function or do PHP side and assign the results. |
okey thank you for answersing |
|
Back to top |
|
|