Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: if statement echoing both 'if' and 'else'?

  1. #1
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Unhappy if statement echoing both 'if' and 'else'?

    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)
    Code:
    <?
    /* 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!

  2. #2
    Join Date
    Mar 2008
    Posts
    122
    Thanks
    17
    Thanked 5 Times in 5 Posts

    Default

    Um, try this:

    PHP Code:
    while ($fris mysql_fetch_assoc($get)){ 
    Instead of this:
    PHP Code:
    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
    Last edited by hmsnacker123; 04-21-2010 at 03:55 PM.

  3. #3
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    thanx for the reply mate, i gave it a try but still no joy. this making my brain hurt lol

  4. #4
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    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!

  5. #5
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Could try this way I think seems like a hack to me though so you might want to wait for a better answer

    PHP Code:
    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>");

    Corrections to my coding/thoughts welcome.

  6. #6
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Angry

    i gave ur method a shot but it was still doing the same

    i played around a bit, and now i got this:
    PHP Code:
    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.

  7. #7
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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>";
    Corrections to my coding/thoughts welcome.

  8. #8
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    still happens when i remove the else statement. do u think it could be my query?
    PHP Code:
    $get mysql_query"SELECT * FROM `friends` WHERE `username` = '$session->username' "); //gets friends
    $fris mysql_fetch_array($get); 

  9. #9
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    bump

  10. #10
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    bump again

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •