如何在社交网站上获取朋友的最新评论

发布于 2024-10-18 02:59:50 字数 2222 浏览 2 评论 0原文

我正在建立一个像 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 获取“较旧的评论”时,问题就出现了。

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

我尝试了 2 种方法来修复,但我无法处理所有错误。

  1. 我将在 JavaScript 中保存所有显示的主评论 ID,然后当我请求旧评论时将其发送到 AJAX,在旧评论的查询中,我将防止主评论和子评论拥有其 ID 为的父评论显示的主要评论 ID。所以我可以防止这个错误。

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

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

但我只能在我的朋友个人资料中获取最新的主评论和子评论,并且无法获取有我朋友的最新子评论的主评论。我又摔倒了!

所以我要求这个页面的最佳解决方案。

I'm building a social network site like Facebook. Just like some part. I used PHP & MySQL. I've problem with the index page, where I'll show all newest comment action of my friends.

Let me expand the database.

Table comments:

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.

Table users:

user_id
...some user info fields...

Table friendship

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)

First, I'll get all my friend 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
    ";

Then I'll get all friend id into an array.

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

Now I've a friend ID array. Then I'm trying to get a limited newest comments.
I'll get a limited newest comments, whenever it's sub comment or main comment.

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

Now I've a sorted newest sub and main comments array (sorted base in datetime) and if this is a sub comment, I'll get the main comment of this comment, then get all sub comment. If this is a main comment, I'll get all sub comment.

At the end I'll get an array contains all main and sub comments which was sorted as begin.
Here's an image demo.
http://rongcon.info/demo/abc/newst.PNG

Here will be a duplicate main comment problem when there are many newest sub comment in a main comment. But I can fixed it easy.

I got my goal. But the problem caused when I'm trying to get "older comments" by AJAX.

The problem is in the older comments, sometimes I'll get a sub-comment which in the main comment which was displayed in the first request. So I'll display a duplicate main comment and it's a bug which I need to Fixed!

I'm tried 2 way to fix but I can't handle all bugs.

  1. I'll save all shown main comment ID in JavaScript, then I'll send it into AJAX when I request older comments, In the query of older comment, I'll prevent the main comment and sub comment have it's parent which have ID as the shown main comment ID. So I can prevent this bug.

But When I'm trying to get more and more older comment. The numbers of shown main comment ID will be long and I'm scared that'll a big problem for performance.

  1. I used the logic of show topic have newest reply in forum. I'll add a "last_comment_date" into each main comment. Then I'll get newest comment based only in main comment, not sub comment. So this is going to basic pagination logic and I'll not get duplicate comment when I show older comment.

But I can only get the newest main comment and sub comment in my friend profile and Can't get the main comment which have a newest sub comment of my friend. I'm falling again!

So I'm asking for a best solution for this page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唯憾梦倾城 2024-10-25 02:59:50

与其选择所有评论(包括子评论),不如首先选择父评论...(其中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

然后对于每个父级,去获取子评论,这样当从ajax获取较旧的评论时,您将不会获得没有父级的子评论片段。

不过可能会慢一些...

Instead of selecting all the comments (including subcomments) maybe, select only parent comments first... (where 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

Then foreach parent, go and get the subcomments, that way when fetching the older comments from ajax, you will not get fragments of subcomments without their parents.

It might be slower though...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文