dongmi5177 2016-09-24 16:50
浏览 20
已采纳

无法从相应的回复php获取db的所有评论

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?

enter image description here

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!

  • 写回答

1条回答 默认 最新

  • doubei8168 2016-09-24 17:16
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler