I have nested comments on my site and it gets longer and longer with replies.
I want to hide/Toggle replies, so I created a nested function which displays comments on parent_id recursively, and I will add ajax letter on.
I want to make it work with post_id call.
Here is the function :
$sql = "SELECT * FROM comments";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$MultiComments = array(
'comments' => array(),
'parent_comments' => array()
);
while ($row = $stmt->fetch()) {
$MultiComments['comments'][$row['comment_id']] = $row;
$MultiComments['parent_comments'][$row['comment_parent_id']][] = $row['comment_id'];
}
function listComments($parent, $comment){
$html = "";
if (isset($comment['parent_comments'][$parent])) {
$html .= "<ul>\n";
foreach ($comment['parent_comments'][$parent] as $comment_id) {
if (!isset($comment['parent_comments'][$comment_id])) {
$html .= "<li class='hasNoChild'>" . $comment['comments'][$comment_id]['comment'] . "</li>";
} else {
$html .= "<li class='HasChild'>" . $comment['comments'][$comment_id]['comment'];
$html .= listComments($comment_id, $comment);
$html .= "</li>";
}
}
$html .= "</ul>\n";
}
return $html;
}
And this is how I call it with parent id :
echo listComments(0,$MultiComments);
Thanks for helps