Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for PHP Comment System With Replies
aaronesteban1
aaronesteban1

Posted on

     

PHP Comment System With Replies

I have code that is able to capture the initial comment and the 1st level of replies, but it doesn’t seem to capture the reply to a reply. I know that it requires an indefinite code using some form of recursion, but not quite sure how to properly implement it. Here's the code I'm using:

<?php
$conn = new mysqli('localhost', 'root', 'Jordan123', 'commentsystem2');

$sql1 = "SELECT * FROM comments WHERE r_to = 0";
$result1 = $conn->query($sql1);

while($row1 = $result1->fetch_assoc()) {
$c_id = $row1['id'];
$c_name = $row1['name'];
$c_comment = $row1['comment'];

   echo '   <div>      <div>ID# '.$c_id.'</div>      <div>'.$c_name.'</div>      <div>'.$c_comment.'<br><br></div>   </div>   ';   $sql2 = "SELECT * FROM comments WHERE r_to = $c_id";   $result2 = $conn->query($sql2);      while($row2 = $result2->fetch_assoc()) {          $r_id = $row2['id'];          $r_name = $row2['name'];          $r_comment = $row2['comment'];          $r_to = $row2['r_to'];          echo '          <div>             <div>ID# '.$r_id.'</div>             <div>'.$r_name.' replied to '.$c_name.'</div>             <div>'.$r_comment.'<br><br></div>          </div>          ';      }//end of 1st while loop that captures comments.

}//end of 1st while loop that captures comments.

$conn->close();
?>

Notice how some of the replies to replies that are in the table, are missing on output.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Interested in web design and web development
  • Location
    Tampa Bay, Florida
  • Work
    Web Developer at Home
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp