V Posted July 23, 2010 Share Posted July 23, 2010 I'm working in a subscription script. I have a list of posts and subscribe buttons next to each one. When a logged in user clicks on a "subscribe" button it should change color and display an "Already Subscribed!" alert if the user tries to click again. It does all that but for some reason, the number of "Subscribe" buttons is multiplied to the number of posts the user is subscribed to.. :-\ so for example if the user subscribes to 4 posts, 4 subscribe buttons show (one of which has a changed color and produces the "already subscribed!" alert.. so I know that part works) I think the flaw is somewhere in the loop, but I can't detect where.. I'm hoping someone can help me out please! The code has some CSS styling but I simplified it for an easy read. There is also a JS that inserts data into the DB, changes button color, and alerts the user which I didn't include <?php session_start(); //get username else NULL $username = empty($_SESSION['valid_user']) ? NULL : $_SESSION['valid_user']; //DB connection //queries posts and the category each belongs to $sql = "SELECT posts.*, categories.* FROM posts, categories WHERE posts.cat_id = categories.cat_id ORDER BY post_date DESC"; $post_result = $connection->query($sql) or die(mysqli_error($connection)); while ($row = $post_result->fetch_assoc()) { $post_name=$row['post_name']; $cat_name = $row["cat_name"]; echo $post_name; echo $cat_name; //query subscription data $sql = "SELECT * FROM subscriptions WHERE username = '$username'"; $subscriptions_result = $connection->query($sql) or die(mysqli_error($connection)); while ($row = $subscriptions_result->fetch_assoc()) { $followed_post = $row['followed_post']; //the name of the posts the user is subscribed to if ($followed_post == $post_name) { //if user is subscribed to post ?> <div class="subscribed_button"> <a href="" class="subscribed" id="<?php echo $post_name; ?>" name="subscribed">Subscribe to Post</a></div> <?php } elseif ($followed_post !== $post_name) { ?> <div class="unsubscribed_button"> <a href="" class="unsubscribed" id="<?php echo $post_name; ?>" name="unsubscribed">Subscribe to Post</a></div> <?php } ?> <?php }//end while loop for subscriptions }//end while loop for posts ?> Not sure if it would have helped, but Itried joining all 3 tables in one query to have just 1 while loop but that didn't work, I get mysql errors Quote Link to comment https://forums.phpfreaks.com/topic/208688-while-loop-within-another-while-loop/ Share on other sites More sharing options...
V Posted July 24, 2010 Author Share Posted July 24, 2010 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/208688-while-loop-within-another-while-loop/#findComment-1090682 Share on other sites More sharing options...
jcbones Posted July 24, 2010 Share Posted July 24, 2010 Try it using this query $sql = "SELECT DISTINCT(followed_post) FROM subscriptions WHERE username = '$username'"; Quote Link to comment https://forums.phpfreaks.com/topic/208688-while-loop-within-another-while-loop/#findComment-1090695 Share on other sites More sharing options...
V Posted July 24, 2010 Author Share Posted July 24, 2010 jcbones thanks so much for the reply! I did some reading about DISTINCT and used it but I get the same effect. Quote Link to comment https://forums.phpfreaks.com/topic/208688-while-loop-within-another-while-loop/#findComment-1090724 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.