PHP&中的嵌套注释的MySQL [英] Nested comments in PHP & MySQL
问题描述
我在论坛上进行了搜索,但没有得到权威的答案.我想以这种方式实现嵌套的注释结构:
I searched through the forums but couldn't get an authoritative answer. I want to implement a nested comment structure in a way like this:
<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的.请帮帮我.
I know how to use mysql_query()
and while()
loops. A beginner in PHP & MySQL. Please help me out.
推荐答案
我为我的博客文章做了类似的事情.但是,我只是尝试了相同的数据.当您说嵌套评论时,最好使用以下方式使用嵌套功能:
I had done something similar for my blogpost. Yet, I just tried out with the same data. When you say nested comments it is better you can use nested functions this way:
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并尝试了此操作:
To make it this way, I have imported your data to MySQL and tried this:
<?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);
?>
正如我在使用嵌套函数之前所说的那样,并且正如您所问的那样,这种方式的输出几乎是相同的(不带花括号):
As I said before I have used nested functions, and as you asked the output is almost same (without the braces) this way:
<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>
希望这会有所帮助.
这篇关于PHP&中的嵌套注释的MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!