Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.83 KB | None | 0 0
  1. <?php
  2.  
  3. //fetch_comment.php
  4.  
  5. //$connect = new PDO('mysql:host=localhost;dbname=tbl_comment', 'root', '');
  6.  
  7. $connect = mysqli_connect('localhost','root','','tbl_comment');
  8.  
  9.  
  10. $query = "
  11. SELECT * FROM tbl_comment
  12. WHERE parent_comment_id = '0'
  13. ORDER BY comment_id DESC
  14. ";
  15.  
  16. $statement = $connect->prepare($query);
  17.  
  18. $statement->execute();
  19.  
  20. $result = $statement->fetchAll();
  21.  
  22.  
  23. $output = '';
  24. //
  25.  
  26. foreach($result as $row)
  27. {
  28.  $output .= '
  29. <div class="panel panel-default">
  30.  <div class="panel-heading">By '.$row["comment_sender_name"].' on '.$row["date"].'</div>
  31.  <div class="panel-body">'.$row["comment"].'</div>
  32.  <div class="panel-footer" align="right"><button type="button" class="btn btn-default reply" id="'.$row["comment_id"].'">Reply</button></div>
  33. </div>
  34. ';
  35.  $output .= get_reply_comment($connect, $row["comment_id"]);
  36.  
  37.  
  38. echo $output;
  39. }
  40. function get_reply_comment($connect, $parent_id = 0, $marginleft = 0)
  41. {
  42.  $query = "
  43. SELECT * FROM tbl_comment WHERE parent_comment_id = '".$parent_id."'
  44. ";
  45.  $output = '';
  46.  $statement = $connect->prepare($query);
  47.  $statement->execute();
  48.  $result = $statement->fetchAll();
  49.  $count = $statement->rowCount();
  50.  if($parent_id == 0)
  51.  {
  52.   $marginleft = 0;
  53.  }
  54.  else
  55.  {
  56.   $marginleft = $marginleft + 48;
  57.  }
  58.  if($count > 0)
  59.  {
  60.   foreach($result as $row)
  61.   {
  62.    $output .= '
  63.   <div class="panel panel-default" style="margin-left:'.$marginleft.'px">
  64.    <div class="panel-heading">By '.$row["comment_sender_name"].' on '.$row["date"].'</div>
  65.    <div class="panel-body">'.$row["comment"].'</div>
  66.    <div class="panel-footer" align="right"><button type="button" class="btn btn-default reply" id="'.$row["comment_id"].'">Reply</button></div>
  67.   </div>
  68.   ';
  69.    $output .= get_reply_comment($connect, $row["comment_id"], $marginleft);
  70.   }
  71.  }
  72.       return $output;
  73. }  
  74.  
  75. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement