erol_smsr
erol_smsr

Reputation: 1496

PHP in_array() function doesn't work in all cases

So I have this weird problem I can't solve, I don't know what the problem is and how I should solve it.

I have a simple system that loads images, called snaps. Every snap, has likes. This is how I load the snaps and the associated (number of) likes:

/*
* Get the snaps
*/
try {
    $select_snaps_query = '
    SELECT snap.snap__id, snap.snap__user__id, snap.snap__4_3_loc, snap.snap__caption, snap.snap__time, user.user__id, user.user__username, user.user__profile_picture
    FROM snap
    JOIN user ON(user.user__id = snap.snap__user__id)
    WHERE snap__user__id IN(
        SELECT followed__user__id
        FROM follow
        WHERE follower__user__id = :follower__user__id
    ) ORDER BY snap__time DESC';
    $prep_select_snaps = $conn->prepare($select_snaps_query);
    $prep_select_snaps->bindParam(':follower__user__id', $user__id, PDO::PARAM_STR);
    $prep_select_snaps->execute();
    $snaps_result = $prep_select_snaps->fetchAll();
    $snaps_count = count($snaps_result);
}   
catch(PDOException $e) {
    $conn = null;
    header('Location: http://www.scrapll.com');
}

if($snaps_count != 0) {
    foreach($snaps_result AS $snaps_row) {
        $snap__id = $snaps_row['snap__id'];   
        $snap__4_3_loc = $snaps_row['snap__4_3_loc'];
        $snap__caption = $snaps_row['snap__caption'];
        $snap__time = $snaps_row['snap__time'];
        $snap__user__id = $snaps_row['snap__user__id'];
        $snap__username = $snaps_row['user__username'];
        $snap__profile_picture = $snaps_row['user__profile_picture'];

        if($snap__profile_picture == null) {
            $snap__profile_picture = 'default.fw.png';   
        }

        $like_row = array();

        // load the likes for each snap
        try {
            $select_like_query = '
            SELECT like__id, like__user__id
            FROM `like`
            WHERE like__snap__id = :like__snap__id';
            $prep_select_like = $conn->prepare($select_like_query);
            $prep_select_like->bindParam(':like__snap__id', $snap__id, PDO::PARAM_STR);
            $prep_select_like->execute();
            $like_result = $prep_select_like->fetchAll();
            $like_count = count($like_result);
        }   
            catch(PDOException $e) {
            $conn = null;
            header('Location: http://www.scrapll.com');
        }

        if(isset($like_result)) {
            foreach($like_result AS $like_row) {
                echo $like__user__id = $like_row['like__user__id'] . '<br/>';   
            }
        }
    }

I load the likes inside the foreach loop of the result set of the snaps.

So, what I need to do is to check if the logged in user has liked a snap. If so, a different unlike button has to appear. However, with the code I have now, it appears for one user who has a liked a snap, but when another user likes the same snap too, one of the two 'likers' doesn't get the unlike button, even though he/she has liked it.

Here's my code to determine if the currently logged in user has already liked the snap. If so, a different markup will show, otherwise, the default snap with a like button shows:

// if liked by currently logged in user
if(in_array($user__id, $like_row)) {
            echo '
            <div class="snap_item">
                <div class="snap_item_following_info">
                    <img class="snap_item_following_img" src="res/stat/img/user/profile/small/'.$snap__profile_picture.'" alt="@'.$snap__username.'" />
                    <a class="snap_item_following_name" href="me.html?id='.$snap__user__id.'">@'.$snap__username.'</a>
                    <!--<div class="snap_too">

                    </div>-->
                </div>
                <img class="snap_img" src="'.$snap__4_3_loc.'" alt="'.$snap__id.'" />
                <div class="like_heart"></div>
                <div class="snap_info">
                    <div class="snap_text">'.$snap__caption. 'LIKED BY ME</div>
                    <div class="snap_sub_info">
                        <span class="snap_time">'.time_ago($snap__time).'</span>
                        <div class="like unlike" style="background: red;">
                            <div class="like_icon liked"></div>
                               <div class="like_no_active" style="color: #FFF">'.$like_count.'</div>
                        </div>
                    </div>
                </div>
            </div>';     
        }
        // if NOT liked by the currently logged in user
        else {
            echo '
            <div class="snap_item">
                 <div class="snap_item_following_info">
                    <img class="snap_item_following_img" src="res/stat/img/user/profile/small/'.$snap__profile_picture.'" alt="@'.$snap__username.'" />
                    <a class="snap_item_following_name" href="me.html?id='.$snap__user__id.'">@'.$snap__username.'</a>
                    <!--<div class="snap_too">

                    </div>-->
                </div>
                <img class="snap_img" src="'.$snap__4_3_loc.'" alt="'.$snap__id.'" />
                <div class="like_heart"></div>
                <div class="snap_info">
                    <div class="snap_text">'.$snap__caption.'</div>
                    <div class="snap_sub_info">
                        <span class="snap_time">'.time_ago($snap__time).'</span>
                        <div class="like" style="background: white;">
                            <div class="like_icon"></div>
                            <div class="like_no_active">'.$like_count.'</div>
                        </div>
                    </div>
                </div>
            </div>';   
        } 

As you can see, I use the in_array() function to see if the user id of the currently logged in user ($user__id) is present in the array of the result set of the likes query.

Does anyone see the problem in my code?

EDIT

A var_dump() shows the following:

array(2) { ["like_id"]=>string(3) "170"
["like__user__id"]=>string(1) "2" }

It shows this similar dumb for each snap. For another snap it says:

array(2) { ["like_id"]=>string(3) "171"
["like__user__id"]=>string(1) "2" }

These appear on snaps that don't show the unlike button while it should. This dump is from a user's page which does correctly show the unlike button:

array(2) { ["like_id"]=>string(3) "170"
["like__user__id"]=>string(1) "2" }

This is what the var_dump() shows

Upvotes: 0

Views: 92

Answers (1)

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41820

If the second piece of code you posted comes right after the first piece, then it is using the $like_row from the last iteration of the foreach loop, and it will only ever work for if the current user matches the user of the last like row returned by the query.

Instead, in the first part, check to see if the current user likes the post while you are looping over the set of likes.

$this_user_likes = false;
if(isset($like_result)) {
    foreach($like_result AS $like_row) {
        if ($like_row['like__user__id'] === $user__id) $this_user_likes = true;
    }
}

Then in the second part where you use

if (in_array($user__id, $like_row)) {...

you may instead use the result of the earlier check.

if ($this_user_likes) {...

Incidentally, it looks like in the second part you are echoing a lot of the same html in the if and else parts. You could just have the repeated part once, and then put only the parts that are different in the if...else. Just a suggestion.

Upvotes: 1

Related Questions