I have a system of comments. Each comment may receive replies, and each reply may receive replies, ad nauseam.
Thus, my database contains a table named "comments" with the following important fields:
id
text
reply_to
...under reply_to, of course, goes the id of the comment to which it is a reply.
So now, the problem is simple: how do I display all the comments, but knowing that after each comment must come the replies to it, and after each reply must come the replies to the reply, etc?
What I tried the most, and what I keep coming back at, is something like this:
$query = mysql_query("SELECT * FROM comments WHERE reply_to=0");
while ($comment = mysql_fetch_array($query))
include("comment.php");
And in comment.php, what I have is:
foreach ($comment as $key = $value) $$key = $value;
echo $text;
echo "<div style='margin-left:30px;'>"; //A margin for a little indent
$subquery = mysql_query("SELECT * FROM comments WHERE reply_to=$id");
while ($comment = mysql_fetch_array($subquery))
include("comment.php");
echo "</div>";
But if I reproduced correctly the essence of my code, the problem is this: that after the first reply is echoed, it goes on to the first reply of the first reply, and then the first reply of the first reply of the first reply, but the loop never gets to the second reply of anything. So for example, supposing the table had 3 comments, each of which had 3 replies, and each of which had 3 replies, etc, the above code would output:
Comment
First reply
First second-order reply
First third-order reply
...
I hope I have explained it clearly enough. inb4: I cannot add new columns to the table.