-2

我在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
?>

我的代码工作正常,但是如何修改它以使其仅打印我朋友的朋友,而这些朋友恰好不是我的朋友?(因为我朋友的一些朋友,恰好也是我的朋友)。

4

1 回答 1

0

对于您的问题,我只知道一种解决方案,当然,它不是那么快,但可行。您可以在inner while中查看用户的友谊>>

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
于 2013-08-27T16:03:31.157 回答