Log in

View Full Version : if statement echoing both 'if' and 'else'?



liamallan
04-21-2010, 02:24 PM
im having a little problem with my users profile pages and an if statement on the profile page!
i have a link on profile pages to add the user as a friend which should disappear and change to 'you are friends' when the request is accepted.

the problem is, it echoes both the link and 'you are friends'!

here is my code:
(the problem area is highlighted in italics)

<?
/* Requested Username error checking */
$req_user = trim($_GET['user']);
if(!$req_user || strlen($req_user) == 0 ||
!eregi("^([0-9a-z])+$", $req_user) ||
!$database->usernameTaken($req_user)){
die("Username not registered");
}

/* Logged in user viewing own account */
if(strcmp($session->username,$req_user) == 0){
echo '<font size=\'6\' color=\'#0C009D\'><b>My Profile</b></font><br><br>';
}
/* Visitor not viewing own account */
else{
$req_user_info = $database->getUserInfo($req_user);
echo '<font size=\'6\' color=\'#0C009D\'><b>'.$req_user_info['username'].'\'s Profile</b></font><br>';
echo "<a href='".$req_user_info['fburl']."' title=\"View ".$req_user_info['username']."'s Facebook Profile\" target=\"_blank\"><img src=\"Facebook_icon.gif\" width=\"25\" height=\"25\" border=\"0\" /></a>&nbsp&nbsp";
echo "<a href=\"usermsg.php?username=".$req_user_info['username']."\" title=\"Send ".$req_user_info['username']." A Message\"><img src=\"comment.png\" width=\"25\" height=\"25\" border=\"0\" /></a>&nbsp&nbsp";
echo "<a href=\"mailto:".$req_user_info['email']."\" title=\"Send ".$req_user_info['username']." An Email\"><img src=\"email.png\" width=\"25\" height=\"25\" border=\"0\" /></a>&nbsp&nbsp";

$get = mysql_query( "SELECT * FROM `friends` WHERE `username` = '$session->username' "); //gets friends
while ($fris = mysql_fetch_array($get)){
if ($fris['friendname'] == $req_user_info['username']){ //checks if user is already a friend
echo ( "You are friends" );
}
else{
echo ( "<a href='friendrequest.php?user=$req_user_info[username]'>Add as Friend</a>");
}
}
}

/* Display requested user information */
$req_user_info = $database->getUserInfo($req_user);
/* Avatar (assumes you have a default for folks who don't upload an avatar, here it is called "no_avatar.gif")*/
if($req_user_info['avatar'] == 0){
echo '<img src="include/userimg/no_avatar.jpg" width="150" height="150"/>';
}
else{
echo '<img src="include/userimg/'.$req_user_info['avatar'].'" width="150" height="150"/>';
}
/* Username */
echo "<br><br><font color='#003399'><b>Username:</b></font><br> ".$req_user_info['username']."<br><br>";

/* Email */
echo "<font color='#003399'><b>Email:</b></font><br> ".$req_user_info['email']."<br><br>";

/* country */
echo "<font color='#003399'><b>Location:</b></font><br> ".$req_user_info['city'].", ".$req_user_info['country']."<br><br>";

/* Games */
echo "<font color='#003399'><b>My Games:</b></font><br> ".$req_user_info['games']."<br><br>";

/* About */
echo "<font color='#003399'><b>About Me:</b></font><br> <em>".$req_user_info['about']."<br><br></em>";

$timestamp = $req_user_info['timestamp'];

echo "<br><font color='#003399'><b>Last Active:</b></font><br> ".date('jS F Y \a\\t g.ia', $timestamp)." <br><br>";

/**
* Note: when you add your own fields to the users table
* to hold more information, like homepage, location, etc.
* they can be easily accessed by the user info array.
*
* $session->user_info['location']; (for logged in users)
*
* ..and for this page,
*
* $req_user_info['location']; (for any user)
*/

/* If logged in user viewing own account, give link to edit */
if(strcmp($session->username,$req_user) == 0){
echo "<br>[<a href=\"useredit.php\">Edit Profile</a>]<br>";
}

/* Link back to main */
echo "<br>Back To [<a href=\"index.php\">Main</a>]<br>";

?>
any suggestions?
your help is appreciated! :)

hmsnacker123
04-21-2010, 03:43 PM
Um, try this:



while ($fris = mysql_fetch_assoc($get)){


Instead of this:


while ($fris = mysql_fetch_array($get)){


Link:
http://uk3.php.net/mysql_fetch_assoc

Failing that, check that $fris['friendname'] or $req_user_info['username'] definitely has a value.

Hope i helped, if you have anymore problems please post them :P

liamallan
04-21-2010, 04:01 PM
thanx for the reply mate, i gave it a try but still no joy. this making my brain hurt :mad: lol

liamallan
04-21-2010, 04:09 PM
i have noticed that on a friends profile, it echoes a link to become friends and also echoes 'you are friends' but when you view a non-friends profile, it echos 2 links to become friends! so it kinda still works lol but not the way i want it!:confused:

bluewalrus
04-21-2010, 05:06 PM
Could try this way I think seems like a hack to me though so you might want to wait for a better answer



if ($fris['friendname'] == $req_user_info['username']){ //checks if user is already a friend
echo ( "You are friends" );
}
if ($fris['friendname'] != $req_user_info['username']){ //checks if user isn't a friend
echo ( "<a href='friendrequest.php?user=$req_user_info[username]'>Add as Friend</a>");
}

liamallan
04-21-2010, 05:50 PM
i gave ur method a shot but it was still doing the same :mad:

i played around a bit, and now i got this:

if ($fris['friendname'] == $req_user_info['username']){ //checks if user is already a friend
echo "[<font color='#666666'>You are friends</font>]";
} else {
echo ( "<a href=\"friendrequest.php?user=".$req_user_info['username']."\" title=\"Become Friends With ".$req_user_info['username']."\"><img src=\"friends_icon.png\" width=\"25\" height=\"25\" border=\"0\" /></a><br>");
}
it works to a certain extent, but only with one of my friends, the rest still give me the option to send them friend request.

bluewalrus
04-21-2010, 06:49 PM
try not using the parenthesis

echo "<a href=\"friendrequest.php?user=".$req_user_info['username']."\" title=\"Become Friends With ".$req_user_info['username']."\"><img src=\"friends_icon.png\" width=\"25\" height=\"25\" border=\"0\" /></a><br>";

liamallan
04-21-2010, 07:00 PM
still happens when i remove the else statement. do u think it could be my query?

$get = mysql_query( "SELECT * FROM `friends` WHERE `username` = '$session->username' "); //gets friends
$fris = mysql_fetch_array($get);

liamallan
04-21-2010, 10:13 PM
bump :D

liamallan
04-23-2010, 12:15 AM
bump again :D

liamallan
04-25-2010, 01:38 PM
this is proving tougher than i thought! i have tried allsorts but nothing is working.

this is what i have now:

if ($fris['friendname'] != $req_user_info['username']){ //checks if user isnt a friend
echo "<a href=\"friendrequest.php?user=".$req_user_info['username']."\" title=\"Become Friends With ".$req_user_info['username']."\"><img src=\"friends_icon.png\" width=\"25\" height=\"25\" border=\"0\" /></a><br>";
}
else{
echo "[<font color='#666666'>You are friends</font>]";
}
problem with this is, it echoes 'you are friends' with one of my friends, and the option to add as friend with the rest of my friends. its like the query isnt selecting all from table but query looks ok, here it is:

$get = mysql_query( 'SELECT * FROM `friends` WHERE `username` = \''.$username.'\''); //gets friends
$fris = mysql_fetch_array($get);
anybody got any ideas?

bluewalrus
04-25-2010, 04:03 PM
echo the results

liamallan
04-25-2010, 04:45 PM
i tried echoing $fris and $fris['friendname'] and both no results. all i want to do is select all the friend name column from table friends where the session username is in column 'username'

liamallan
04-25-2010, 04:50 PM
i dunno if this is anything to do with it, but i am trying to achieve this within an existing else thingy

liamallan
04-25-2010, 09:29 PM
no worries guys, i finally sussed it

now i have this:

$getFriends = mysql_query("SELECT * FROM `friends` WHERE `friendname` = \"".$req_user_info['username']."\" AND `username` = \"".$session->username."\"");
if (mysql_num_rows($getFriends) == 0){
echo "<a href=\"friendrequest.php?user=".$req_user_info['username']."\" title=\"Become Friends With ".$req_user_info['username']."\"><img src=\"friends_icon.png\" width=\"25\" height=\"25\" border=\"0\" /></a><br>";
}
else {
echo ( "<br>[<font color='#666666'>You are friends</font>]<br>");
}
thanx for all your help anyway