1

I am looking to exclude a specific value from my foreach output where it occurs in either friend_one or friend_two. I do not want to exclude this from my query.

The parameter to exclude would be $profile_user where it occurs in either friend_one or friend_two. I tried to do this if(in_array($friend_total_row->friend_one == $profile_user)), but I am getting errors with it, plus it only has friend_one in it.

Anyone have an idea how I can do this?

<?php           
    //Friends --- total
    $friend_status = 2;
    $friend_sql = "
        SELECT *
        FROM friends
        WHERE (friend_one = :profile_user or friend_two = :profile_user)
        AND status = :total_status
    ";
    $friend_stmt = $con->prepare($friend_sql);
    $friend_stmt->execute(array(':profile_user' => $profile_user, ':total_status' => $friend_status));
    $friend_total_rows = $friend_stmt->fetchAll(PDO::FETCH_ASSOC);
    $count_total_friend = $friend_stmt->rowCount();
?>  
        <div id="friend-list-container">
            <div id="friend-list-count">Friends <span class="light-gray"><?php echo $count_total_friend; ?></span></div>
            <div id="friend-list-image-container">
<?php           
    foreach ($friend_total_rows as $friend_total_row) {
        if(in_array($friend_total_row->friend_one == $profile_user)) {
            continue;
        }
        else {
        $friend_1   = $friend_total_row['friend_one'];
        $friend_2   = $friend_total_row['friend_two'];
        //$friend_status        = $friend_total_row['status'];
        //$friend_status_date = $friend_total_row['date'];
        }
        echo $friend_1 . $friend_2;
    }
?>

I am only wanting to exclude the $profile_user from outputting, which is where the X is. Then I want the squared output to show. enter image description here

3
  • 1
    this line if(in_array($friend_total_row->friend_one == $profile_user)) is wrong. you have to separate by comma, not two equal signs. php.net/manual/en/function.in-array.php Commented Nov 28, 2016 at 20:13
  • Just to ask why do you not want to exclude this in the query. Also, with the current setup you would be getting duplicate rows on the instance that the friend is profile is that something you expect? Commented Nov 28, 2016 at 20:22
  • @nerdlyist I do not want to exclude from the query because the query is matching relationships. IE: If the profile user is in the same record with another user, that is a friendship. Within this output, I am wanting to exclude the profile user to simply show their friends. Commented Nov 28, 2016 at 20:25

1 Answer 1

1

Updated answer :

foreach ($friend_total_rows as $friend_total_row) {
    $friend_1   = $friend_total_row['friend_one'];
    $friend_2   = $friend_total_row['friend_two'];

    if($friend_1 !== $profile_user) {
        echo $friend_1;
    }

    if($friend_2 !== $profile_user) {
        echo $friend_2;
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

The first example you gave is giving errors for -> and the second, in_array example is giving Notice: Trying to get property of non-object in errors.
For array - Parse error: syntax error, unexpected '$friend_total_row' (T_VARIABLE), expecting '] ... the comparison is not giving errors for the brackets [ as well.
hmmm.. no longer any errors, but it is not outputting anything. Does this exclude the entire row from outputting? I am only wanting to exclude the $profile_user from outputting. I added an image to help understand.
Humm.. Just understood what you are trying to achieve :)
Perfect. Thank you! Sorry if my question wasn't very clear. Do you know if there is a function that could randomize the results? I can look it up, just do not know if there is or not.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.