简体   繁体   中英

php friend system, display friends of my friends

I have a table named friends, keeps my friends in a php friend system. The table is as follows:

friend_id | user_one | user_two

My script is the following for displaying my friends.

$check_friend_query = mysql_query("  SELECT friends_id from friends WHERE  (user_one='$session_user_id' AND user_two ='$user_id') OR (user_one='$user_id' AND user_two='$session_user_id')   ");

if( mysql_num_rows($check_friend_query) == 1  ){
      echo" 1st degree friend";
}

All I want is to display 2nd degree friends. My 2nd degree friends are the friends of my friends. Any idea how to do this?

You can run an original query to grab all of your friends (like you have), then another to find their friends using their user ID as the variable to look for.

mysql_query("SELECT friends_id from friends WHERE user_one='$user_id'");

Something like this would check friends_id for all friends where the name is the user_id of the person.

Although the variables are a bit difficult to figure out, since I don't know the script.

Join the friends against itself:

SELECT degree2.*
FROM friends
LEFT JOIN friends AS degree2 ON friends.user_two = degree2.user_one
WHERE friends.user_one = 'original friend';

For further degrees, you'd just add another join level, eg

LEFT JOIN friends AS degree3 ON degree2.user_two = degree3.user_one
LEFT JOIN friends AS degree4 on degree3.user_two = degree4.user_ond
etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM