January 13, 201115 yr Okay so I'm building a CMS, and to properly support posts' ability to be in multiple categories I have set up my DB with 3 tables as follow: blog_posts / blog_cats / blog_posts_cats To match up the blogs posts one or more categories I link them in the blog_posts_cats. Now to recall this from the database I have created the following loop: $query = mysql_query("SELECT * FROM blog_posts ORDER BY id DESC") or die(mysql_error()); if(mysql_num_rows($query) == 0){ echo "No posts were found."; } else { while($q = mysql_fetch_assoc($query)) { $title = $q['Title']; $id = $q['ID']; echo "<li>$title in"; $query2 = mysql_query("SELECT * FROM blog_posts, blog_cats, blog_posts_cats WHERE blog_posts_cats.cat_id = blog_cats.id AND blog_posts.id = $id") or die(mysql_error()); while($q2 = mysql_fetch_assoc($query2)) { $catName = $q2['Title']; echo " $catName,"; } echo "</li>"; } } The issue with this is that instead of merely posting the categories that belong to the specific post, it accumulates them all to show on each post. The echo should look like this: post1 in cat1 cat2 post2 in cat3 cat4 But instead looks like this: post1 in cat1 cat2 cat3 cat4 post2 in cat1 cat2 cat3 cat4 How do I resolve this?
Create an account or sign in to comment