Freddy
Freddy

Reputation: 867

If statement not being executed when it is expected to work

I have the following three tables - users_thoughts, users and post_favourites. I have a feature in my social networking site which allows users to favourite a post (posts are stored in user_thoughts and when a post is favourited i.e. when favourite_post.php is called, it will store the favourites in post_favourites table).

If the logged in user has NOT favourited a post, it will show the Glyphicon heart-empty. But if the user HAS favourited a post, it will show Glyphicon-heart.

Lets assume I have the following rows in my tables:

users table:

id: 1
first_name: conor

id: 2
first_name: Alice

id: 3
first_name: Anderson

user_thoughts table:

id: 100
message: This is a post by Alice.
added_by: Alice

id: 101
message: This is a post by Anderson
added_by: Anderson

post_favourites table:

id: 1
user_id: 1 (This is the id of the user who has favourited the post, see users table)
thought_id: 101 

Assume I am logged in as Conor. As you can see Conor has favourited Anderson's post, so Glyphicon-heart should appear, as logged in user has already favourited Anderson's post. But Glyphicon-heart-empty is appearing, even though my database says conor has favourited anderson's post.

Here are my queries:

Note: $username is the session variable created for the logged in user.

$count = mysqli_query ($connect, "SELECT * FROM user_thoughts");
while ($row = mysqli_fetch_assoc($get_thoughts_from_db)) {
    $thought_id      = $row['id'];
}
    // Get all user_ids attachted to a thought ($thought_id)
    $get_user_id = mysqli_query ($connect, "SELECT * FROM post_favourites WHERE thought_id = '$thought_id'");
        $id_fetch = mysqli_fetch_assoc ($get_user_id);
        $all_user_id = $id_fetch ['user_id'];
        $post_id = $id_fetch ['thought_id'];

    // get id of users from users table
    $get__id = mysqli_query ($connect, "SELECT id FROM users WHERE username = '$username'");
        $id_fetch2 = mysqli_fetch_assoc ($get__id);
        $logged_in_user = $id_fetch2 ['id'];    

if ($post_id == $thought_id){
    // If the post has already been favourited by the username, then display this icon with funtionality.   
         if ($all_user_id == $logged_in_user){
                echo "$get_num_of_favs
                <a href='/inc/unfavourite_post.php?id=";?><?php echo $thought_id;?><?php echo "'>
                        <span class='glyphicon glyphicon-heart' aria-hidden='true' style='padding-right: 5px;'></span> 
                    </a>";
                 }
            // if the post hasn't been favourited by the username, display this icon.
            else {
                echo "$get_num_of_favs
                <a href='/inc/favourite_post.php?id=";?><?php echo $thought_id;?><?php echo "'>
                        <span class='glyphicon glyphicon-heart-empty' aria-hidden='true' style='padding-right: 5px;'></span> 
                    </a>";
            }
         }

I need it so already favourited posts by the user who is logged in are dislayed with heart icon i.e. the if statement is executed. But at the moment, Anderson's post of This is a post by Anderson- which is favourited by Conor, is showing with heart-empty - meaning the else statement is being executed and I don't know why?

Upvotes: 1

Views: 49

Answers (2)

Shadow
Shadow

Reputation: 34254

The whole complex logic in php can be substituted by using joins in the sql query to combine data from all relevant tables:

select *
from user_thoughts u
left join post_favourites p on u.id=p.thought_id and u.user_id=$_SESSION['userid']

$_SESSION['userid'] is the user id of the currently logged on user. Store it in session along with the user name.

When you loop through the resultset in php, if thought_id=='' (empty string), then you know that the current user did not like this post yet.

Upvotes: 0

Tim Mickey
Tim Mickey

Reputation: 361

I can't be sure without your data, but I would put my money on it being that more than one user has favorited this post. Then, the first result returned by

$get_user_id = mysqli_query ($connect, "SELECT * FROM post_favourites WHERE thought_id = '$thought_id'");

will not have the user_id of the current logged in user. You should add the $logged_in_user to the WHERE clause:

$get_user_id = mysqli_query ($connect, "SELECT * FROM post_favourites WHERE thought_id = '$thought_id' AND user_id = '$logged_in_user'");

(Don't forget to escape any data before querying! )

Upvotes: 0

Related Questions