I have a "friends" apparatus similar to Facebook's. If two users are friends, there is an entry in my 'friends' table, with user1 as the first user's ID, and user2 as the second user's ID.
I just wrote a function to get all of a user's friends and return them in an array. It works like this:
Code: Select all
$id = $_SESSION['user_id'];
$result = mysql_query("SELECT * FROM `friends` WHERE `user1`='$id' OR `user2`='$id'");
while($row = mysql_fetch_assoc($result)) {
if($row['user1'] == $id) {
$friends[] = $row['user2'];
}
else {
$friends[] = $row['user1'];
}
}
return $friends;
This works fine, except I want to order the friends by first name. Normally I'd just join the friends table with my users table, and get the first name that way. However, there are two different 'user' columns in my friends table, and I'd have to join the users table to both of these columns. What's the best way of doing this while being able to differentiate between user1's info and user2's info, as taken from the joined users table?
Thanks.