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 inside a foreach..

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


Joined: 17 Jan 2007
Posts: 4

PostPosted: Wed Jan 17, 2007 8:56 pm    Post subject: Foreach inside a foreach.. Reply with quote

I have two mysql tables, categories (a category has more articles) and articles (an article belongs to a category, using the category as a foreign key).

How can i output the categories using a foreach statement and inside each category all the articles belonging to that category.. for example
Code:
Category 1
    name of article belonging to category 1
    another article
    yet another..
Category 2
    article belonging to category2

Category3
    and so on..


I know how to this in pure php, but i just can't figure it out in smarty, I also found a few posts with similar problems, but none has managed to enlighten me..
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Wed Jan 17, 2007 9:03 pm    Post subject: Reply with quote

It really depends on your data structures. If the nested list is embedded as a field in each record of the master list, then the solution should be obvious. Perhaps describe the structure that you are having problems with. Otherwise, try searching the forums as this has been answered before. Useful search terms would be "nested loop".
Back to top
View user's profile Send private message
Pap
Smarty Regular


Joined: 21 Jun 2006
Posts: 69
Location: Denver, CO

PostPosted: Thu Jan 18, 2007 4:48 pm    Post subject: Reply with quote

Do these not help?
http://www.phpinsider.com/smarty-forum/viewtopic.php?p=38428#38428
http://www.phpinsider.com/smarty-forum/viewtopic.php?p=38485#38485
_________________
Don't be stupid, be a Smarty™.
Come and join the P-H-Party.
Back to top
View user's profile Send private message
viseztrance
Smarty n00b


Joined: 17 Jan 2007
Posts: 4

PostPosted: Sat Jan 20, 2007 8:54 am    Post subject: Reply with quote

This is the code i've used after reading those two posts:

Code:
$query = "select...";

while($object = mysql_fetch_assoc($query)) {
         $this->show_all[$object['c_name']][] = $object;
}


$object['c_name'] is the name of a category

and it works very nice (and thank you, by the way Smile), but how can i output the id of the category (category's primary key) as well ? right now i can only view it's name
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