针对线索评论的可扩展解决方案

发布于 2024-08-19 05:36:19 字数 520 浏览 7 评论 0原文

我不知道如何在 PHP 和 MySQL 中创建一个可以一次处理数百条评论的线程评论系统。

像这样的东西是我唯一能想到的

$query = execute_query...('SELECT * FROM `comments` WHERE `post` = "1"');
foreach($query as $comment){
    $replies = execute_query...('SELECT * FROM `comment_replies` WHERE `comment` = "' . $comment['id'] . '"');
    if($replies){
        echo $comment['body']; //....
        foreach($replies as $reply){ /*....*/ }
    }
    else{
        echo $comment['body'];
    }
}

所以我需要有关数据库结构的提示以及如何在考虑性能的情况下检索所有线程评论:)

I'm not sure how to create a threaded comments system in PHP and MySQL which can handle hundreds of comments at a time.

Something like this is the only thing I can come up with

$query = execute_query...('SELECT * FROM `comments` WHERE `post` = "1"');
foreach($query as $comment){
    $replies = execute_query...('SELECT * FROM `comment_replies` WHERE `comment` = "' . $comment['id'] . '"');
    if($replies){
        echo $comment['body']; //....
        foreach($replies as $reply){ /*....*/ }
    }
    else{
        echo $comment['body'];
    }
}

So I need tips on database structure and how I can retrive the all the threaded comments with performance in mind please :)

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

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

发布评论

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

评论(3

难如初 2024-08-26 05:36:19

I'm sure you'll find the article Managing Hierarchical Data in MySQL helpful. Those two tables can be easily merged into one

逆光飞翔i 2024-08-26 05:36:19

为什么不加入 comments 和 comment_replies 表呢?

然后只需添加一个额外的生成字段来显示它是评论还是comment_reply。并在 foreach 中使用 if 来选择它们,例如:

if($type == 'comment') 
{
      //do something with the comment
}
elseif($type == 'comment_reply')
{
      //do something with the comment reply
}

同时检查评论 ID 是否发生变化,以便将它们分开。

Why not join the comments and the comment_replies table?

then just add a extra generated field that shows if its a comment or a comment_reply. And select them with a if in a foreach like:

if($type == 'comment') 
{
      //do something with the comment
}
elseif($type == 'comment_reply')
{
      //do something with the comment reply
}

Also check if the comment id changes so you can seperate them.

看春风乍起 2024-08-26 05:36:19

我会选择邻接列表模型而不是嵌套集模型,因为使用嵌套集模型,我必须在表上完成的每个 INSERT 和 DELETE 操作上构建整个树结构,如果我有 1000 条记录,对于单个 INSERT 或DELETE 操作必须更新所有记录,需要更多时间来执行。
我的数据库结构是:
在此处输入图像描述

我还用 php mysql jquery 和 ajax 编写了一个小型线程评论系统。看看 http://pradipchitrakar.com.np/programming/threaded-comment -系统/

I would choose Adjacency List Model over Nested Set Model because with Nested Set Model, I would have to build the whole tree structure on every INSERT and DELETE operation done on the table and if I have 1000′s of records, for a single INSERT or DELETE operation all the records has to be updated taking more time to execute.
My database structure would be:
enter image description here

Also I have written a small threaded comment system with php mysql jquery and ajax. Take a look at http://pradipchitrakar.com.np/programming/threaded-comment-system/

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