Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Need help with mysql while loop

Featured Replies

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.