I am trying to create a system where depending on what user is a)logged in or b)if the username is found in the array.
If you are logged in and in the friend_array of the person, you should see the 'Unfriend' button. If you're on the page which is your own profile, everything is hidden If your not on your page and not in the array, 'Add Friend'.
Unfriend seems to be working, but it just seems to be defaulting 'Add Friend' whenever I' on my profile:
$cool = $_GET['id'];
$evan = mysql_query("SELECT * FROM users WHERE username='{$_SESSION['user_login']}'");
$joe1 = mysql_fetch_assoc($evan);
$cookie = mysql_query("SELECT * FROM users WHERE id='{$_GET['id']}'");
$joe = mysql_fetch_assoc($cookie);
$i = 0;
$berbs = $joe['username'];
error_reporting(E_ERROR | E_PARSE);
if (in_array($_SESSION['user_login'], $friendArray)) {
$addAsFriend = '<input type="submit" name="removefriend" value="UnFriend">';
echo $addAsFriend;
}
error_reporting(E_ERROR | E_PARSE);
if ($_GET['id'] == $joe1['id']) {
$addAsFriend = '<input style="display:none;" type="submit" name="addfriend" value="Add Frimm1end">';
echo $addAsFriend;
}
error_reporting(E_ERROR | E_PARSE);
if (!in_array($joe1['username'], $friendArray) || !$_GET['id'] == $joe1['id']) {
$addAsFriend = '<input type="submit" name="addfriend" value="Add Friend">';
echo $addAsFriend;
}
!$_GET['id']
will most likely always evaluate to TRUE. The following line:
if (!in_array($joe1['username'], $friendArray) || !$_GET['id'] == $joe1['id'])
Could be read
if (!in_array($joe1['username'], $friendArray) || TRUE == $joe1['id'])
You probably want
if (!in_array($joe1['username'], $friendArray) || $_GET['id'] != $joe1['id'])
Explanation PHP's dynamic type casting will take any positive integer or non-empty string and evaluate them to TRUE if you attempt a boolean expression such as !$_GET['id']
. You have another boolean expression testing if TRUE == $joe1['id']
, which will always be true unless $joe1['id']
is empty.
As a side note, you can prevent this kind of typecasting from occurring by using the Identical and Not Identical operators (=== and !==).
if (false == 0) {
echo "This will be true";
}
if (true == 1) {
echo "This will also be true";
}
if (false === 0) {
echo "This will never echo";
}
if (true === 1) {
echo "This will also never echo";
}