<section class="comments-section" id="comments">
<h2 class="h2 center">Partagez vos temoignages </h2>
<p class="paragraph center">Laissez la haine et le mépris à l'extérieur ! Ici on écrit que des choses positives.</p>
<div class="container">
<form method="POST" id="comment_form">
<div class="form-group">
<input type="text" name="comment_name" id="comment_name" class="form-control" placeholder="Nom ou pseudo" />
</div>
<div class="form-group">
<textarea style="resize: none;" name="comment_content" id="comment_content" class="form-control" placeholder="Redigez votre commentaire" maxlength="700" rows="10"></textarea>
</div>
<div class="form-group">
<input type="hidden" name="comment_id" id="comment_id" value="0" />
<input type="submit" name="submit" id="submit" class="btn btn-success" value="Publier" />
</div>
</form>
<span id="comment_message"></span>
<br />
<div class="all-comments" id="all">
<div class="content" id="display_comment"></div>
</div>
</section>
I have comment system on my website and I would like to add Load More button to hide comments and show only the first three and to load the rest of comments when clicked on button. These comments are from my DB. I have tried a lot of exemples but I'm still stucking...
Have you any solution please? Thanks
<?php
//fetch_comment.php
$connect = new PDO('mysql:host=localhost;dbname=testing', 'root', '');
// select from DB
$query = "
SELECT * FROM comments
WHERE parent_comment_id = '0'
ORDER BY comment_id DESC LIMIT 3
";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$output = '';
foreach($result as $row)
{
$output .= '
<div class="panel panel-default">
<div class="panel-heading">Par <b>'.$row["comment_sender_name"].'</b> le <i>'.$row["date"].'</i></div>
<div class="panel-body">'.$row["comment"].'</div>
<div class="panel-footer" align="right"><button type="button" style="font-size:1rem;"class="btn btn-default reply" id="'.$row["comment_id"].'">Répondre</button></div>
</div>
';
$output .= get_reply_comment($connect, $row["comment_id"]);
}
echo $output;
function get_reply_comment($connect, $parent_id = 0, $marginleft = 0)
{
$query = "
SELECT * FROM comments WHERE parent_comment_id = '".$parent_id."'
";
$output = '';
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$count = $statement->rowCount();
if($parent_id == 0)
{
$marginleft = 0;
}
else
{
$marginleft = $marginleft + 48;
}
if($count > 0)
{
foreach($result as $row)
{
$output .= '
<div class="panel panel-default" style="margin-left:'.$marginleft.'px">
<div class="panel-heading">By <b>'.$row["comment_sender_name"].'</b> le <i>'.$row["date"].'</i></div>
<div class="panel-body">'.$row["comment"].'</div>
<div class="panel-footer" align="right"><button type="button" style="font-size:1rem;" class="btn btn-default reply" id="'.$row["comment_id"].'">Répondre</button></div>
</div>
';
$output .= get_reply_comment($connect, $row["comment_id"], $marginleft);
}
}
return $output;
}
?>