【问题标题】:Nested comments in PHP & MySQLPHP & MySQL 中的嵌套注释
【发布时间】:2012-07-28 10:25:09
【问题描述】:

我搜索了论坛,但没有得到权威的答案。我想以这样的方式实现嵌套的注释结构:

<ul>
    <li>This is the parent first comment!
        <ul>
            <li>This is the reply for the first parent comment!
                <ul>
                    <li>This is a reply for the first reply of the parent comment!</li>
                    <li>This is a third reply for the first parent comment!</li>
                </ul>
            </li>
            <li>This is another reply for first parent comment!</li>
        </ul>
    </li>
    <li>This is gonna be parent second comment!
        <ul>
            <li>This is a reply for the second comment!</li>
        </ul>
    </li>
    <li>This is fourth parent comment!</li>
</ul>

我的表的转储如下:

+----+------------------------------------------------------------+--------+
| id | text                                                       | parent |
+----+------------------------------------------------------------+--------+
|  1 | This is the parent first comment!                          |      0 |
|  2 | This is gonna be parent second comment!                    |      0 |
|  3 | This is the reply for the first parent comment!            |      1 |
|  4 | This is another reply for first parent comment!            |      1 |
|  5 | This is a reply for the first reply of the parent comment! |      3 |
|  6 | This is a reply for the second comment!                    |      2 |
|  7 | This is a third reply for the first parent comment!        |      3 |
|  8 | This is fourth parent comment!                             |      0 |
+----+------------------------------------------------------------+--------+

我知道如何使用 mysql_query()while() 循环。 PHP & MySQL 的初学者。请帮帮我。

【问题讨论】:

标签: php mysql


【解决方案1】:

有几种不同的方法可以在 MySQL 中存储分层数据。查看 Bill Karwin 的 presentation,它展示了四个选项。

  • 邻接列表
  • 路径枚举
  • 嵌套集
  • 闭包表

您正在使用邻接列表模型来存储分层数据,但不幸的是,这是您可以选择用于查询子树的最难模型。

您的选择是:

  • 更改为其他型号。
  • 将查询限制为 n 级深度。
  • 使用存储过程递归查询。有关这方面的更多信息,请参阅 Quassnoi 的系列文章 - Hierarchical queries in MySQL

【讨论】:

  • 你说的邻接表和图模型有关系吗?
  • 是的。 OPs结构可以看成是一棵树(没有环的图)。
  • 更改模型如?你说的是数据库表结构吗?
  • @Aishu:是的。使用您选择的模型以外的任何模型,您的任务都会很容易。
【解决方案2】:

我为我的博文做了类似的事情。然而,我只是尝试了相同的数据。当您说 nested cmets 时,最好以这种方式使用 nested functions

function getComments($parent)
{
    # Get the data from SQL.
    # Check if it has nested comments.
    if (hasComments())
        getComments($child); # $child is the ID of the current comment.
}

为了做到这一点,我已将您的数据导入 MySQL 并尝试了以下操作:

<?php
    mysql_connect('localhost', 'root');
    mysql_select_db('nestedcomments');
    function getComments($parent)
    {
        $res = mysql_query("SELECT * FROM `nestcomm` WHERE `parent` = $parent");
        if (mysql_num_rows($res))
        {
            echo "<ul>\n";
            while (($dat = mysql_fetch_array($res)) !== false)
                echo "<li>", $dat["text"], getComments($dat["id"]), "</li>\n";
            echo "</ul>\n";
        }
        else
            echo ($parent === 0) ? 'No Comments!' : "";
    }
    getComments(0);
?>

正如我之前所说,我使用了嵌套函数,并且正如你所问的那样,输出几乎相同(没有大括号):

<ul>
<li>This is the parent first comment!<ul>
<li>This is the reply for the first parent comment!<ul>
<li>This is a reply for the first reply of the parent comment!</li>
<li>This is a third reply for the first parent comment!</li>
</ul>
</li>
<li>This is another reply for first parent comment!</li>
</ul>
</li>
<li>This is gonna be parent second comment!<ul>
<li>This is a reply for the second comment!</li>
</ul>
</li>
<li>This is fourth parent comment!</li>
</ul>

希望这会有所帮助。

【讨论】:

  • 看起来这就是我想要的。让我实施和检查。
  • 请注意,虽然这会给出正确的结果,但它会触发每个评论的一个查询,所以它会很慢。但是,如果您只是将其作为一个实验,或者对于一个用户和 cmets 很少的网站,它可能对您来说很好。
  • 请不要使用mysql_* 函数编写新代码。它们不再维护,社区已经开始deprecation process。看到 red box 了吗?相反,您应该了解prepared statements 并使用PDOMySQLi。如果你不能决定哪一个,this article 会帮助你。如果你选择 PDO,here is good tutorial.
  • @PraveenKumar:PDO 是较新的,从 5.1 开始,它默认包含
  • @PraveenKumar:从 mysql 转换到 mysqli 更容易,所以对于旧项目,这样做。学习 PDO 并在较新的项目中使用它。
猜你喜欢
  • 1970-01-01
  • 2015-01-27
  • 2011-07-12
  • 2011-10-11
  • 2010-11-22
  • 2014-10-12
  • 1970-01-01
  • 1970-01-01
  • 2013-08-11
相关资源
最近更新 更多