I did something similar for my blogpost. However, I just tried the same data. When you say nested comments , it is best to use nested functions as follows:
function getComments($parent) {
To do this, I imported your data into 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, I used nested functions, and, as you requested, the output is almost the same (without bindings) as follows:
<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>
Hope this helps.
Praveen kumar
source share