I have 2 tables in my DB, comments and replies. I am joining the 2 tables in my sql statement but cannot seem to get all comments with corresponding replies.
PHP -
$sql = "SELECT comments.comment_id, comments.user, comments.comment, comments.date, comments.post_id, replies.reply, replies.username, replies.replyDate, replies.com_id FROM comments LEFT JOIN replies ON comments.comment_id = replies.com_id WHERE comments.post_id=$id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$first = true;
while($row = $result->fetch_assoc()) {
$date = $row['date'];
$mydate = date("M jS g:i a",strtotime($date));
$rdate = $row['replyDate'];
$user = $row['user'];
$username = $row['username'];
$comment = $row['comment'];
$reply = $row['reply'];
$comID = $row['comment_id'];
$post_id= $row['post_id'];
if ($first) {
echo '<div id="comuser">'.$user.': </div>';
echo '<div id="icomment">'.$comment.'</div>';
echo '<div id="comdate">'.$mydate.'</div>';
echo '<div class="replyBTN">reply</div>';
echo '<form method="post" class="replyForm" action="get_reply.php?reply='.$comID.'&&title='.$title.'&&post='.$post_id.'">';
echo '<input type="text" id="addReply" name="addReply" placeholder="add reply">';
echo '</form>';
$first = false;
}
echo '<div id="replyCont">';
echo '<div id="replyUser">'.$username.'</div>';
echo '<div id="replyDate">'.$rdate.'</div>';
echo '<div id="reply">'.$reply.'</div>';
echo '</div>';
}
} else {
echo "<div id='noCom'>no comments..</div>";
}
With help from Bramar in a previous question I am able to get all replies for said comment but it only shows the first comment. Do I need to write a function to call to keep looping for the rest of the comments or do I need a totally different method all together?
When looking at the image it should show comment 2, 3 etc.. but stops after the first. I know this is because the $first being true then called false after the first loop but thats the only way I can see to stop it duplicating the same comment for the second and third reply..
Any ideas? Thanks!
something along these lines:
if ($comID!=$prevComID) {
echo '<div id="comuser">'.$user.': </div>';
echo '<div id="icomment">'.$comment.'</div>';
echo '<div id="comdate">'.$mydate.'</div>';
echo '<div class="replyBTN">reply</div>';
echo '<form method="post" class="replyForm" action="get_reply.php?reply='.$comID.'&&title='.$title.'&&post='.$post_id.'">';
echo '<input type="text" id="addReply" name="addReply" placeholder="add reply">';
echo '</form>';
$prevComID = $comID;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.