Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在社交网站上获得我朋友的最新评论_Php_Mysql_Networking_Social - Fatal编程技术网

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

Php 如何在社交网站上获得我朋友的最新评论,php,mysql,networking,social,Php,Mysql,Networking,Social,我正在建立一个像facebook这样的社交网站。就像某个部分。我使用了php和mysql。我对索引页面有问题,在那里我会显示我朋友的所有最新评论动作 让我来扩展数据库 表1评论: 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

我正在建立一个像facebook这样的社交网站。就像某个部分。我使用了php和mysql。我对索引页面有问题,在那里我会显示我朋友的所有最新评论动作

让我来扩展数据库

表1评论:

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)
首先,我要拿到我所有的朋友身份证

$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";
现在我有了一个排序的最新子注释和主注释数组(按日期时间排序的基),如果这是一个子注释,我将获取该注释的主注释,然后获取所有子注释。如果这是一个主要的评论,我会得到所有的子评论

"select * from comments where author_id IN ('".implode("','",$all_friend_id)."' order by datetime desc limit 0 , 20";
最后,我将得到一个数组,其中包含所有按begin排序的主注释和子注释。 这是一个图像演示。

当主注释中有许多最新的子注释时,将出现重复的主注释问题。但是我可以很容易地把它修好

我实现了我的目标。但是当我试图通过ajax获取“旧评论”时,问题就出现了

问题在于旧的注释,有时我会在第一个请求中显示的主注释中得到一个子注释。所以我将显示一个重复的主注释,这是一个我需要修复的错误

我尝试了两种方法来修复,但我不能处理所有的错误。 1.我将在javascript中保存所有显示的主注释ID,然后在请求旧注释时将其发送到ajax,在查询旧注释时,我将阻止主注释和子注释的父注释ID作为显示的主注释ID。因此,我可以防止此错误

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

  • 我用了展示主题的逻辑在论坛上有了最新的回复。我将在每个主要评论中添加一个“最后的评论日期”。然后我会得到最新的评论,只在主评论,而不是子评论的基础上。这是基本的分页逻辑,当我显示旧的注释时,我不会得到重复的注释
  • 但是我只能在我的朋友档案中获得最新的主评论和子评论,而不能获得包含我朋友最新子评论的主评论。我又跌倒了

    因此,我要求为本页提供最佳解决方案。每个建房的人都能帮助我吗?
    非常感谢你

    可能不选择所有注释(包括子注释),而只选择父注释。。。(其中parent_id=0)

    然后,对于每个父节点,去获取子节点,这样当从ajax获取旧的注释时,如果没有父节点,就不会得到子节点的片段


    它可能会慢一些,但是…

    不是选择所有注释(包括子注释),而是首先只选择父注释。。。(其中parent_id=0)

    然后,对于每个父节点,去获取子节点,这样当从ajax获取旧的注释时,如果没有父节点,就不会得到子节点的片段


    可能会慢一些…

    只是出于好奇,你决定从头开始创建社交网站有什么特别的原因吗?有很多不错的框架(特定于php mysql社交网络),可以为您处理社交网络的很多方面。只是出于好奇,您决定从头开始创建社交网站有什么特别的原因吗?有很多不错的框架(特定于php mysql社交网络),可以为您处理社交网络的许多方面。感谢您的解决方案。但是它不能得到我朋友评论的非朋友的评论。谢谢你的解决方案。但是它不能得到我朋友评论的非朋友的评论。