PHP和PHP中的嵌套注释MySQL的

时间:2012-07-28 10:25:09

标签: php mysql

我在论坛中搜索但无法获得权威答案。我想以这样的方式实现嵌套的注释结构:

<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和PHP的初学者MySQL的。请帮帮我。

2 个答案:

答案 0 :(得分:12)

在MySQL中存储多种不同的方法来存储分层数据。查看Bill Karwin的presentation,其中有四个选项。

  • 邻接清单
  • 路径枚举
  • 嵌套集
  • 关闭表

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

nested sets query subtree

您的选择是:

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

答案 1 :(得分:2)

我为我的博客做了类似的事情。然而,我只是尝试了相同的数据。当你说嵌套评论时,你可以用这种方式使用嵌套函数

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>

希望这会有所帮助。