1

我正在建立一个像 facebook 这样的社交网站。就像某个部分一样。我用过php&mysql。我的索引页面有问题,我将在其中显示我朋友的所有最新评论操作。

让我扩展数据库。

表评论:

comment_id 
content
datetime
author_id //who created this comment
profile_id //the user_id of the owner profile which this comment commented on.
parent_id //the comment id which is parent of this comment id. if the parent_id = 0 , so this comment is the main comment.

表用户:

user_id
...some user info fields...

表友谊

user_id // who added another as friend.
friend_id // who was added as friend by another.
status // the status of this friendship, 0 is invited and 1 is alreay friend (accepted)

首先,我会得到我所有的朋友ID。

$query = "select DISTINCT friend_id as friend from friendship where user_id = ".$user_id." and status = 1
    union
    select DISTINCT user_id as friend from friendship where friend_id = ".$user_id." and status = 1
    ";

然后我会将所有朋友的 id 放入一个数组中。

foreach($total_friends as $each_friend){
        $all_friend_id[] = $each_friend['friend'];
    }

现在我有一个朋友 ID 数组。然后我试图获得有限的最新评论。无论是副评论还是主评论,我都会收到有限的最新评论。

"select * from comments where author_id IN ('".implode("','",$all_friend_id)."' order by datetime desc limit 0 , 20";

现在我有一个排序的最新子评论和主评论数组(按日期时间排序),如果这是一个子评论,我会得到这个评论的主要评论,然后得到所有的子评论。如果这是主要评论,我会得到所有子评论。

最后,我将得到一个数组,其中包含所有按开始排序的主注释和子注释。这是一个图像演示。 http://rongcon.info/demo/abc/newst.PNG

当主评论中有许多最新的子评论时,这将是一个重复的主评论问题。但我可以很容易地解决它。

我得到了我的目标。但是当我试图通过ajax获取“旧评论”时引起的问题。

问题出在较旧的评论中,有时我会在第一个请求中显示的主要评论中得到一个子评论。所以我会显示一个重复的主评论,这是一个我需要修复的错误!

我尝试了两种修复方法,但我无法处理所有错误。1.我会将所有显示的主评论ID保存在javascript中,然后在我请求旧评论时将其发送到ajax,在旧评论的查询中,我会阻止主评论和子评论有它的父评论ID 作为显示的主要评论 ID。所以我可以防止这个错误。

但是当我试图获得越来越多的老评论时。显示的主要评论 ID 的数量会很长,我担心这会对性能造成很大的问题。

  1. 我使用了显示主题的逻辑在论坛中有最新的回复。我将在每个主要评论中添加一个“last_comment_date”。然后,我将仅在主评论中获得最新评论,而不是子评论。所以这将是基本的分页逻辑,当我显示较旧的评论时,我不会得到重复的评论。

但是我只能在我的朋友资料中获得最新的主评论和副评论,并且无法获得有我朋友最新副评论的主评论。我又掉下去了!

所以我要求这个页面的最佳解决方案。每个建造的人都可以帮助我吗?太感谢了!

4

1 回答 1

1

与其选择所有评论(包括子评论),不如先选择父评论...(其中 parent_id=0)。

select * from comments where parent_id=0 and author_id IN ('".implode("','",$all_friend_id)."' order by datetime desc limit 0 , 20

然后 foreach parent,去获取子评论,这样当从 ajax 获取旧评论时,你不会得到没有父评论的子评论片段。

不过可能会慢一些...

于 2011-02-21T09:32:16.843 回答