【问题标题】:How to get newest comments of my friend in social network website如何在社交网络网站上获取我朋友的最新评论
【发布时间】:2011-02-21 02:34:30
【问题描述】:

我正在建立一个像 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 数组。然后我试图获得有限的最新 cmets。 无论是副评论还是主评论,我都会获得有限的最新 cmets。

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

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

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

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

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

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

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

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

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

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

所以我要求为此页面提供最佳解决方案。每个建造的人都可以帮助我吗? 非常感谢!

【问题讨论】:

  • 出于好奇,您决定从头开始创建社交网站有什么特别的原因吗?有很多不错的框架(特定于 php-mysql-social network),可以为您处理社交网络的许多方面。

标签: php mysql networking social


【解决方案1】:

可能不是选择所有 cmets(包括子 cmets),而是先选择父 cmets...(其中 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,去获取subcmets,这样当从ajax 获取较旧的cmets 时,你不会得到没有父母的subcmets 片段。

虽然可能会慢一些...

【讨论】:

  • 感谢您的解决方案。但它无法得到我朋友评论的非朋友的评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-13
  • 1970-01-01
相关资源
最近更新 更多