I am creating a friend system. I have a database table called 'friends'. The only time this table has records inserted into it is when a user attempts to add a friend or accept a friend request. Therefore friend relations are not default.
I am attempting to write a query which checks for friend relations, in order to properly set the correct button: Add Friend, Pending Request, Friends, Null.
My issue is I am only checking for a relation, therefore if I go to a user's page that I am not friends with, the query is not seeing the relationship and it causes my variables to be undefined.
Now to the code:
Please note that the parameters in the query's WHERE clause are $user_id and $profile_user.
$user_id is the user logged in.
$profile_user is the profile the logged in user is viewing.
So, if $user_id views his own profile or another user's profile (without relation, ie: sent a friend request), it throws error that the following variables are undefined:
$select_friend_1 = $friend_row['friend_one'];
$select_friend_2 = $friend_row['friend_two'];
$friend_status = $friend_row['status'];
In summary, how can I make my query work, so that a relation is not needed, but it still checks for the relation. I know the issue resides in my WHERE clause - just not sure what to change it to.
$okay = true;
//Checks
if ( $okay ) {
$friend_sql = "
SELECT *
FROM friends
WHERE friend_one = ?
AND friend_two = ?
";
$select_friend_stmt = $con->prepare($friend_sql);
$select_friend_stmt->execute(array($user_id, $profile_user));
$friend_rows = $select_friend_stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($friend_rows as $friend_row) {
$select_friend_1 = $friend_row['friend_one'];
$select_friend_2 = $friend_row['friend_two'];
$friend_status = $friend_row['status'];
$friend_status_date = $friend_row['date'];
}
}
else {
echo "Friend Status not found.";
}
$friend_status_button = null;
if ($friend_status == 2) {
$friend_status_button = "Approved";
}
else if ($friend_status == 1) {
$friend_status_button = "Pending";
}
else if ($select_friend_1 == $select_friend_2) {
$friend_status_button = null;
}
else {
$friend_status_button = "Add Friend";
}
Aucun commentaire:
Enregistrer un commentaire