Total Pageviews

Tree Comment System PHP+MYSQL


Tree Comment System
==================



Status id is 123, for this status we wil display comments.

Comments are arranged with one space replaces with dash/Hyphen


<?php
include('DB/db_conx.php');

$result = mysqli_query($db_conx,"SELECT * FROM status_comment WHERE thread_id = '123' ");

$comments = array();
while ($row = mysqli_fetch_array($result)) {
  $row['thread_id'] = array();
  $comments[$row['id']] = $row;

}


//   into its parent_id
foreach ($comments as $k => &$v) {
  if ($v['parent_id'] != 0) {
    $comments[$v['parent_id']]['thread_id'][] =& $v;
  }
}
unset($v);

// delete the thread_id comments from the top level
foreach ($comments as $k => $v) {
  if ($v['parent_id'] != 0) {
    unset($comments[$k]);
  }
}

// now we display the comments list, this is a basic recursive function
function display_comments(array $comments, $level = 0) {
  foreach ($comments as $info) {
   $com= str_repeat('-', $level + 1).' comment '.$info['id']." ". $info['msg'] ."<br/>";


    echo str_replace("-", "&nbsp", $com);


    if (!empty($info['thread_id'])) {
      display_comments($info['thread_id'], $level + 1);
    }
  }
}

display_comments($comments);


?>




[ If you like my work please donate  :) you can see donate button on top left side ]