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 loops help

 
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
m477
Smarty n00b


Joined: 09 Aug 2006
Posts: 3

PostPosted: Wed Aug 09, 2006 1:45 pm    Post subject: Foreach loops help Reply with quote

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
View user's profile Send private message
flourishing
Smarty Rookie


Joined: 15 Oct 2005
Posts: 6

PostPosted: Wed Aug 09, 2006 3:30 pm    Post subject: Reply with quote

{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
View user's profile Send private message MSN Messenger
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Aug 09, 2006 5:37 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
m477
Smarty n00b


Joined: 09 Aug 2006
Posts: 3

PostPosted: Thu Aug 10, 2006 6:50 am    Post subject: Reply with quote

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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Aug 10, 2006 8:03 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
m477
Smarty n00b


Joined: 09 Aug 2006
Posts: 3

PostPosted: Thu Aug 10, 2006 8:33 am    Post subject: Reply with quote

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
View user's profile Send private message
shannera
Administrator


Joined: 13 Feb 2006
Posts: 802
Location: Edertal, Germany

PostPosted: Thu Aug 10, 2006 12:12 pm    Post subject: Reply with quote

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
View user's profile Send private message
Watchopolis
Smarty Rookie


Joined: 06 Aug 2014
Posts: 5

PostPosted: Tue Aug 26, 2014 10:55 pm    Post subject: Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Aug 27, 2014 3:44 am    Post subject: Reply with quote

its just a php function

http://php.net/array_slice
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Aug 27, 2014 9:01 am    Post subject: Reply with quote

what mohrt says. Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Watchopolis
Smarty Rookie


Joined: 06 Aug 2014
Posts: 5

PostPosted: Wed Aug 27, 2014 10:15 am    Post subject: Reply with quote

messju wrote:
what mohrt says. Smile


does this means we can use php fucntion inside tpl files without using {php} {/php} ?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed Aug 27, 2014 5:55 pm    Post subject: Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Aug 27, 2014 6:53 pm    Post subject: Reply with quote

Watchopolis wrote:
messju wrote:
what mohrt says. Smile


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
View user's profile Send private message Visit poster's website
Watchopolis
Smarty Rookie


Joined: 06 Aug 2014
Posts: 5

PostPosted: Thu Aug 28, 2014 2:16 pm    Post subject: Reply with quote

mohrt wrote:
Watchopolis wrote:
messju wrote:
what mohrt says. Smile


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 Smile
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