在php朋友系统中,如何从我朋友的朋友表朋友打印

时间:2013-08-27 13:23:37

标签: php mysql

我在php中创建了一个朋友系统。一个名为friends的表有3列。首先是friends_id,第二个是user_one,第三个是user_two。朋友(friends_id,user_one,user_two)。其中user_one和user_two是作为朋友的用户的ID。

下面这段代码打印给了我朋友的朋友。

<?php

$friend_query = mysql_query("  SELECT `user_one`, `user_two` FROM `friends` WHERE `user_one`='$session_user_id' OR `user_two`='$session_user_id' ");    

while($run_friend = mysql_fetch_array($friend_query)){

$user_one = $run_friend['user_one'];
$user_two = $run_friend['user_two'];

if($user_one == $session_user_id){
  $user = $user_two;
}else{
  $user = $user_one;
}
$friend_id = getuser($user, 'user_id');


$friend_query_two = mysql_query("  SELECT `user_one`, `user_two` FROM `friends` WHERE (`user_one`='$friend_id' and `user_two` != '$session_user_id') OR (`user_one`!='$session_user_id' and`user_two`='$friend_id' )   ");  


while($run_friend_two = mysql_fetch_array($friend_query_two)){

$user_one_two = $run_friend_two['user_one'];
$user_two_two = $run_friend_two['user_two'];

if($user_one_two == $friend_id){
 $user_two = $user_two_two;
}else{
 $user_two = $user_one_two;
}

$friend_id_two = getuser($user_two, 'user_id');

echo $friend_id_two;

 } //end while first
 } //end while second
?>

我的代码工作正常,但我怎么能修改它才能让它只打印我朋友的朋友,而这些朋友不是我的朋友呢? (因为我朋友的一些朋友也恰好是我的朋友)。

1 个答案:

答案 0 :(得分:0)

我知道你的问题只有一个解决方案,当然,它不是那么快,但可行。 你可以在&gt;&gt;

中检查用户在内心的友谊
while($run_friend_two = mysql_fetch_array($friend_query_two)){
    $user_one_two = $run_friend_two['user_one'];
    $user_two_two = $run_friend_two['user_two'];

    if($user_one_two == $friend_id){
        $user_two = $user_two_two;
    }else{
        $user_two = $user_one_two;
    }
    $friend_id_two = getuser($user_two, 'user_id');
    ///// check for the existence
    $existence = mysql_query("SELECT 'friends_id' FROM 'friends' WHERE (`user_one`='$friend_id_two' and `user_two` = '$session_user_id') OR (`user_one`='$session_user_id' and`user_two`='$friend_id_two' )");
    if(mysql_num_rows($existence)>0) /// check whether the query get results or not
        echo $friend_id_two;
} //end while first