Results 1 to 7 of 7

Thread: problem with friend system 'if' statement

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

    Angry problem with friend system 'if' statement

    i am currently working on integrating a friend system onto my site, all has went well untill i got to the link on users profiles, my aim is to have the link appear if the user viewing the profile is not the profile owners friend, and disappear once the friend request is accepted!

    Here is what i have so far in userinfo.php(profile page):
    PHP Code:
    <?
    /* Requested Username error checking */
    $req_user trim($_GET['user']);
    if(!
    $req_user || strlen($req_user) == ||
       !
    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 
    "<h1>My Profile</h1>";
    }
    /* Visitor not viewing own account */
    else{
       
    $req_user_info $database->getUserInfo($req_user);
       echo 
    "<br>[<a href=\"usermsg.php?username=".$req_user_info['username']."\" >Send ".$req_user_info['username']." a Message</a>]&nbsp";
       echo 
    '[<a href="mailto:'.$req_user_info['email'].'">Send '.$req_user_info['username'].' an E-mail</a>]&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>");

       echo 
    '<h1>'.$req_user_info['username'].'\'s Profile</h1>';
    }
    if(
    $req_user_info['userlevel']=='9'){
        echo 
    "<img src=\"admin_icon.jpg\" width=\"126\" height=\"21\"/><br><br>";
            
    }

    /* Display requested user information */
    $req_user_info $database->getUserInfo($req_user);

    /* Username */
    echo "<b>Username:</b> ".$req_user_info['username']."<br><br>";

    /* Email */
    echo "<b>Email:</b> ".$req_user_info['email']."<br><br>";

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

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

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

    $timestamp $req_user_info['timestamp'];

    echo 
    "<br><b>Last Active:</b> ".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>";

    ?>
    can anyone advise me where i have went wrong? as this is the final hurdle for the friend system. thanx
    Last edited by liamallan; 04-10-2010 at 07:11 PM.

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

    Default

    the 'if' statement in question is:
    PHP Code:
    }
    $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>");


  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I have several basic ideas before trying in depth debugging:

    1. Check (echo/print_r) the vales of both $fris and $req_user_info before the if statement, so that you can see that their values are and see the actual problem (the if statement is not wrong-- the syntax looks right).

    2. A while statement should have brackets around its contents, {}. It is possible to have a one line or one statement expression after a while (or if, or else, etc.) WITHOUT {} brackets, BUT this is limited and can be hard to read. I recommend ALWAYS using {} even if you are only using one line or one statement.
    Rewrite the while structure like this:
    while (...) {
    ...
    }
    THEN see if it works better.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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

    Default

    tried what you said, still get blank screen. im not entirely sure i have done the while suggestion correctly.

    this is what i have now:
    PHP Code:
    }

    while (
    $fris mysql_fetch_array($get)){ 
    $get mysql_query"SELECT * FROM `friends` WHERE `username` = '$session->username' "); //gets friends
    $req_user_info $database->getUserInfo($req_user);}
    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>");


  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I don't understand the new order. Go back to the original code and add the { after while (...), AND the } at the VERY END of the code, after the if and the else. The {} pair surrounds everything that needs to occur within the while statement. Now you are just getting the data within the while, then a single time after that doing an if/else pair, which will effectively operate only on the last value in the while.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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

    Default

    i gave it a bash, and still getting blank screen.

    this is the whole of userinfo.php:
    PHP Code:
    <?
    /* Requested Username error checking */
    $req_user trim($_GET['user']);
    if(!
    $req_user || strlen($req_user) == ||
       !
    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 
    "<h1>My Profile</h1>";
    }
    /* Visitor not viewing own account */
    else{
       
    $req_user_info $database->getUserInfo($req_user);
       echo 
    "<br>[<a href=\"usermsg.php?username=".$req_user_info['username']."\" >Send ".$req_user_info['username']." a Message</a>]&nbsp";
       echo 
    '[<a href="mailto:'.$req_user_info['email'].'">Send '.$req_user_info['username'].' an E-mail</a>]&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>");}
       } 
       echo 
    '<h1>'.$req_user_info['username'].'\'s Profile</h1>';
    }
    if(
    $req_user_info['userlevel']=='9'){
        echo 
    "<img src=\"admin_icon.jpg\" width=\"126\" height=\"21\"/><br><br>";
            
    }

    /* Display requested user information */
    $req_user_info $database->getUserInfo($req_user);

    /* Username */
    echo "<b>Username:</b> ".$req_user_info['username']."<br><br>";

    /* Email */
    echo "<b>Email:</b> ".$req_user_info['email']."<br><br>";

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

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

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

    $timestamp $req_user_info['timestamp'];

    echo 
    "<br><b>Last Active:</b> ".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>";

    ?>
    can u make sure i done the {} thing right

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

    Default

    dont worry, finally sussed it!

    this is how i done it:

    i moved $get and $fris to the very top of the whole page code like this:
    PHP Code:
    <?
    include("include/session.php");
    include(
    "db.php");
    $user $_SESSION['username'];
    $get mysql_query"SELECT * FROM `friends` WHERE `username` = '$session->username' "); //gets friends
    $fris mysql_fetch_array($get);
    ?>
    i scrapped the whole while thing.
    and this is the new if statement:
    PHP Code:
     }
    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>]');
       } 
       {
       echo 
    '<h1>'.$req_user_info['username'].'\'s Profile</h1>';
    }
    if(
    $req_user_info['userlevel']=='9'){
        echo 
    "<img src=\"admin_icon.jpg\" width=\"126\" height=\"21\"/><br><br>";
            


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
  •