|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
viseztrance Smarty n00b
Joined: 17 Jan 2007 Posts: 4
|
Posted: Wed Jan 17, 2007 8:56 pm Post subject: Foreach inside a foreach.. |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Jan 17, 2007 9:03 pm Post subject: |
|
|
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 |
|
Pap Smarty Regular
Joined: 21 Jun 2006 Posts: 69 Location: Denver, CO
|
|
Back to top |
|
viseztrance Smarty n00b
Joined: 17 Jan 2007 Posts: 4
|
Posted: Sat Jan 20, 2007 8:54 am Post subject: |
|
|
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 ), 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 |
|
|
|
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
|