Wall Script的问题

时间:2013-07-30 21:23:57

标签: php mysql

我的墙壁脚本存在问题,一个帖子在用户自己的墙上多次显示,一次在他的朋友墙上显示。例如,如果我有5个朋友,则该脚本将显示单个帖子5次。

由于它在朋友墙上显示一次,这部分工作正常,但问题出在用户自己的墙上。

PHP代码:

$qfu = "SELECT * FROM my_friends WHERE (user_id = '$user_id' OR friend_id = '$user_id') AND status = '1'";
$fu = mysql_query($qfu) or die(mysql_error());
$rowfu = mysql_num_rows($fu);
while ($rowf = mysql_fetch_array($fu)) {
    $user_id   = $rowf['user_id'];
    $friend_id = $rowf['friend_id'];
    $querymsgs = "SELECT * FROM messages where uid_fk = '$friend_id' OR uid_fk = '$user_id' order by msg_id DESC Limit 1";
    $qmsgs = mysql_query($querymsgs) or die(mysql_error());
    $row_msgs = mysql_num_rows($qmsgs);
    while ($msgs = mysql_fetch_array($qmsgs)) {
        $msg_id     = $msgs['msg_id'];
        $orimessage = $msgs['message'];
        $message    = tolink(htmlentities($msgs['message']));
        $time       = $msgs['created'];
        $privacy    = $msgs['privacy'];
        if ($privacy == '0') {
            $privacy = 'Only Me';
        } elseif ($privacy == '2') {
            $privacy = 'Public';
        } else {
            $privacy = 'Friends';
        }
        $uid           = $msgs['uid_fk'];
        $fullname      = $msgs['fname'] . " " . $msgs['lname'];
        $face          = $Wall->Avatar($uid);
        $commentsarray = $Wall->Comments($msg_id);
?>  

我也尝试了下面的代码,但它也显示了多次相同的帖子:

$qfu = "SELECT * FROM my_friends WHERE (user_id = '$user_id' OR friend_id = '$user_id') AND status = '1'";
$fu = mysql_query($qfu) or die(mysql_error());
$rowfu = mysql_num_rows($fu);
while ($rowf = mysql_fetch_array($fu)) {
    if ($user_id == $rowf['user_id']) {
        $fid = $rowf['friend_id'];
    } else {
        $fid = $rowf['user_id'];
    }
    $querymsgs = "SELECT * FROM messages M, tbluser U WHERE U.id='$user_id' and M.uid_fk=U.id and (M.privacy='1' or M.privacy='2') order by M.msg_id desc ";
    $qmsgs = mysql_query($querymsgs) or die(mysql_error());
    $row_msgs = mysql_num_rows($qmsgs);
    $msgs = mysql_fetch_array($qmsgs);
    $msg_id = $msgs['msg_id'];
    $orimessage = $msgs['message'];
    $message = tolink(htmlentities($msgs['message']));
    $time = $msgs['created'];
    $privacy = $msgs['privacy'];
    if ($privacy == '0') {
        $privacy = 'Only Me';
    } elseif ($privacy == '2') {
        $privacy = 'Public';
    } else {
        $privacy = 'Friends';
    }
    $uid = $msgs['uid_fk'];
    $fullname = $msgs['fname'] . " " . $msgs['lname'];
    $face = $Wall->Avatar($uid);
    $commentsarray = $Wall->Comments($msg_id);
?>

请帮我摆脱这个问题。

1 个答案:

答案 0 :(得分:0)

我认为问题出现在这句话中:$qfu = "SELECT * FROM my_friends WHERE (user_id = '$user_id' OR friend_id = '$user_id') AND status = '1'"; - 如果你有5个朋友,你的id将会在结果中出现5次,因此会自己发帖5次。

你应该摆脱查询的user_id = '$user_id'部分:
$qfu = "SELECT * FROM my_friends WHERE friend_id = '$user_id' AND status = '1'"; - 这样你只会得到你的朋友。您已拥有自己的ID,因此无需查询。

现在您需要将用于获取消息的代码放入单独的函数中并准备就绪:

function getMessagesById($authorId) {
    $querymsgs = "SELECT * FROM messages where uid_fk = '$authorId' order by msg_id DESC";
    $qmsgs = mysql_query($querymsgs) or die(mysql_error());
    $row_msgs = mysql_num_rows($qmsgs);
    while ($msgs = mysql_fetch_array($qmsgs)) {
        $msg_id     = $msgs['msg_id'];
        $orimessage = $msgs['message'];
        $message    = tolink(htmlentities($msgs['message']));
        $time       = $msgs['created'];
        $privacy    = $msgs['privacy'];
        if ($privacy == '0') {
            $privacy = 'Only Me';
        } elseif ($privacy == '2') {
            $privacy = 'Public';
        } else {
            $privacy = 'Friends';
        }
        $uid           = $msgs['uid_fk'];
        $fullname      = $msgs['fname'] . " " . $msgs['lname'];
        $face          = $Wall->Avatar($uid);
        $commentsarray = $Wall->Comments($msg_id);
}

//get all own messages
getMessagesById($userId);

//get all friends' messages
$qfu = "SELECT * FROM my_friends WHERE friend_id = '$user_id' AND status = '1'";
$fu = mysql_query($qfu) or die(mysql_error());
$rowfu = mysql_num_rows($fu);
while ($rowf = mysql_fetch_array($fu)) {
    $friend_id = $rowf['friend_id'];
    getMessagesById($friend_id);
}