[eluser]Andy UK[/eluser]
I have a blog which is almost finished, but I'm not sure how to add a list of the categories assigned to the posts at the end of each individual one.
Tables are as follows:
Blog_posts
Categories
Blog_categories_join (many to many relationship)
Now while I have no trouble getting the details from the Blog_posts table such as title, date, body, etc. I can't get my head around how to add another dimension by adding the categories for each post.
For example, let's say the database returns 10 posts. All well and good. I can loop through the results and get the title, body, etc and echo them to the screen.
However, if I want to add a list of the categories at the end of each of the ten posts, It gets complicated. Post one might have been assigned two categories, post three might have five, etc, etc.
Now I'm guessing that I need two different queries, one to get the blog posts and one to get the categories for each post ID. The second would query the Blog_categories_join table, joining the query with the categories table to get the names for each category ID.
Ok, let's say that I now have my two result sets... Where do I go from here? Do I need some kind of loop within a loop in the view, or should I be doing this in the model? I could probably do this is the view with quite a bit of code and trail and error, but I'd be interested to know if there's a better way.
Thanks in advance.