I'm working on a friends list type function that will display a user's friends and I'm having some logic problems. The way the table for storing friends is set up, there are 3 columns; userid1, userid2 and friendstatus. A friendstatus of 1 indicates the users are friends, status of 0 indicates a request is pending. When a user adds another user to be their friend, the user who sends the request is placed in userid1, and the user who receives the request is placed in userid2.
So, the issue that I'm having is getting the function to properly select the user's friends, not the user himself. Basically, I'm trying to get this function to determine which column is the user(me for instance) and which column is the friend.
I originally had the first SQL statement as the following, but this didn't work either.
$friends = mysql_query("Select * from friends where (userid1 = $myUsername OR userid2=$myUsername) AND friendstatus = '1'");
Here's the logic I'm trying to create.
- Get userid(mine)
- Search column 1 for my ID: if found, display column 2(which would be my friend)
- search column 2 for my ID: if found, display column 1(which would be my friend)
The function friendslookup just displays the information and pulls the user's info. It doesn't add to the logic.
$myUsername = $_GET['myFriends'];
if(isset($myUsername)){
$friends = mysql_query("Select * from friends where userid1 = $myUsername AND friendstatus = '1'");
$friends2 = mysql_query("Select * from friends where userid2 = $myUsername AND friendstatus = '1'");
if(mysql_num_rows($friends) > 0 OR mysql_num_rows($friends2) >0 ) {
echo '<table><tr><td>Username </td><td> Location</td></tr>';
$col1rows = mysql_num_rows($friends);
$col2rows = mysql_num_rows($friends2);
}
for ($i=0; $i<$col1rows;$i++){
$myFriends = mysql_query("Select * from friends where userid1 = $myUsername AND friendstatus = '1'");
$friend = mysql_fetch_array($myFriends);
$friend_1 = $friend['userid2'];
friendsLookup($friend_1,$myUsername);
}
for ($i=0; $i<$col2rows;$i++){
$myFriends_2 = mysql_query("Select * from friends where userid2 = $myUsername AND friendstatus = '1'");
$friend = mysql_fetch_array($myFriends_2);
$friend_2 = $friend['userid1'];
friendsLookup($friend_2,$myUsername);
}
}