简体   繁体   English

PHP:快速主题注释问题

[英]PHP: Quick threaded comments question

I'm basically trying to code a simple threaded comments system where users can comment on other user's comment. 我基本上是在尝试编写一个简单的线程注释系统,以便用户可以对其他用户的注释进行注释。 It'll allow only one level of comments. 它只允许一个级别的评论。

The comments table in the database is something like: - id - text - timestamp - parent_id (can be NULL) 数据库中的注释表类似于:-id-文本-时间戳记-parent_id(可以为NULL)

My question is how should I query the comments and their children (comments)? 我的问题是我应该如何查询评论及其子级(评论)? I'm just not sure how they would be places in the array, and then how to loop and output them properly. 我只是不确定它们在数组中的位置,然后如何循环并正确输出它们。

Your help is much appreciated =) 非常感谢您的帮助=)

If its only one level deep, you could get all comments and munge them into the structure you want. 如果仅深入一层,则可以获取所有注释并将其填入所需的结构中。 You could do something like this : 您可以这样做:

function get_comments()
{
    $sql = 'select * from comments order by timestamp';
    $result = $this->db->query($sql)->result();

    $comments = array();
    foreach ($result as $row)
    {
        if ($row->parent_id)
        {
            $comments[$row->parent_id]->children[] = $row;
        }
        else
        {
            $comments[$row->id] = $row;
            $comments[$row->id]->children = array();
        }
    }

    return array_values($comments);
}

Here is a fairly detailed explanation on how to handle this: 这是有关如何处理此问题的相当详细的说明:

http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM