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()循环. PHPPHP的初学者MysqL的.请帮帮我.

我为我的博客做了类似的事情.然而,我只是尝试了相同的数据.当你说嵌套注释时,你可以用这种方式使用嵌套函数
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>

希望这会有所帮助.

相关文章

Hessian开源的远程通讯,采用二进制 RPC的协议,基于 HTTP 传输。可以实现PHP调用Java,Python,C#等多语...
初识Mongodb的一些总结,在Mac Os X下真实搭建mongodb环境,以及分享个Mongodb管理工具,学习期间一些总结...
边看边操作,这样才能记得牢,实践是检验真理的唯一标准.光看不练假把式,光练不看傻把式,边看边练真把式....
在php中,结果输出一共有两种方式:echo和print,下面将对两种方式做一个比较。 echo与print的区别: (...
在安装好wampServer后,一直没有使用phpMyAdmin,今天用了一下,phpMyAdmin显示错误:The mbstring exte...
变量是用于存储数据的容器,与代数相似,可以给变量赋予某个确定的值(例如:$x=3)或者是赋予其它的变...