Jump to content

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/208688-while-loop-within-another-while-loop/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.