Log in

View Full Version : Resolved problem with friend system 'if' statement



liamallan
04-08-2010, 09:59 AM
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):

<?
/* 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 "<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

liamallan
04-08-2010, 08:03 PM
the 'if' statement in question is:

}
$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>");
}

djr33
04-09-2010, 12:35 AM
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.

liamallan
04-09-2010, 10:16 PM
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:

}

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>");
}

djr33
04-10-2010, 05:54 AM
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.

liamallan
04-10-2010, 02:06 PM
i gave it a bash, and still getting blank screen.

this is the whole of userinfo.php:

<?
/* 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 "<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

liamallan
04-10-2010, 04:58 PM
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:

<?
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:

}
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>";

}