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

Limit Foreach Results within a Section Loop

 
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
ShamusMay
Smarty Rookie


Joined: 28 Apr 2015
Posts: 18

PostPosted: Thu May 19, 2016 3:11 pm    Post subject: Limit Foreach Results within a Section Loop Reply with quote

I have a piece of code for my article section that returns the Topic and all Articles associated to that topic. I want to limit the number of articles displayed to only 3 per topic. How can I achieve this?

*I tried using an iteration and break, but it only would show 3 articles on 1 topic.

Code:
{section name=TopicsList loop=$TopicsList}
<p>{$TopicsList[TopicsList].topic_name}</p>
{foreach $ArticlesList as $i}
{if $i.article_topic_id == $TopicsList[TopicsList].topic_id}
<p>{$i.article_title}</p>                     
{/if}
{/foreach}
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu May 19, 2016 7:04 pm    Post subject: Reply with quote

Why you pass more than 3 articles to the template to begin with?
Back to top
View user's profile Send private message
ShamusMay
Smarty Rookie


Joined: 28 Apr 2015
Posts: 18

PostPosted: Thu May 19, 2016 7:16 pm    Post subject: Reply with quote

I guess it is just a grab everything function.

Code:
function getArticlesList() {
  return
  getSqlResult(
    "select * from article order by article_topic_id, article_title",
    SQL_RESULT_ALL);
}


I used to just show all articles, but the article list is getting too big, so I need to limit it now.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu May 19, 2016 8:41 pm    Post subject: Reply with quote

Then fix your PHP code, instead of wasting time trying to do everything in a template.
Next time you'd ask Smarty to make coffee and bake cookies at this rate.
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