Imagine an comment system with an infinite reply structure. Like this:
Comment
  Reply to comment
    Reply to reply to comment
      Reply to reply to reply to comment
        etc....
  Reply to comment
Comment
  Reply to comment
I am trying to think of a structure inside PHP combined with mysql to do this. I thought of something like this:
$query = mysqli_query($link, "SELECT * FROM comments");    
while($comment_array = mysqli_fetch_assoc($query)){
   echo $comment_array['text'];
   $query_reply = mysqli_query($link, "SELECT * FROM comments WHERE reply_id='$comment_array[id]'");
   while($reply_array = mysqli_fetch_assoc($query_reply)){
      echo $reply_array['text'];
      $query_reply2 = mysqli_query($link, "SELECT * FROM comments WHERE reply_id='$reply_array[id]'");
      while($reply_array2 = mysqli_fetch_assoc($query_reply2)){
         echo $reply_array['text'];
         ...... etc.
      }
   }
}
But as you can see there is a problem in this structure. This structure is not infinite and the same bit of code must be repeated a lot of times.
Is there a way to do this more efficiently? Putting a loop somewhere? Making a function, like searchRepliesofComment();?

 
     
     
    