You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现博客评论与对应帖子关联展示及添加评论框

实现评论与对应帖子关联展示及帖子专属评论框

嘿,我来帮你搞定这个帖子和评论关联展示的问题!看起来你现在的代码里帖子和评论是分开输出的,而且评论框没法对应到具体帖子,我给你梳理下修改方案:

先说说现有代码的核心问题

  • getPostInfoo函数直接在内部echo帖子内容,返回的$fields是空数组,导致前端循环根本没作用,也没法把评论和帖子关联起来
  • getComment用了内连接查询,会直接过滤掉没有评论的帖子,而且返回的评论没有绑定到对应帖子的ID
  • 评论框的postid固定写死为1,没法针对每个帖子提交评论

解决方案:重构查询逻辑+前端展示

第一步:修改后端函数,获取带评论的结构化帖子数据

替换原来的getPostInfoo和getComment,写一个新函数一次性获取所有帖子及对应评论:

function getPostsWithComments() {
    // 用左连接确保无评论的帖子也能被查询到
    $q = "SELECT p.*, c.commentid, c.username AS comment_username, c.comment, c.cmntdate 
          FROM ".TBL_POSTS." p 
          LEFT JOIN ".TBL_COMMENTS." c ON p.postid = c.postid 
          ORDER BY p.postid DESC, c.cmntdate DESC";
    $result = mysqli_query($this->connection, $q);
    
    if (!$result) {
        return [];
    }
    
    $posts = [];
    while ($fetch = mysqli_fetch_assoc($result)) {
        $postId = $fetch['postid'];
        // 初始化帖子基础信息(仅第一次遇到该帖子时)
        if (!isset($posts[$postId])) {
            $posts[$postId] = [
                'postid' => $postId,
                'post' => $fetch['post'],
                'username' => $fetch['username'],
                'comments' => []
            ];
        }
        // 如果有评论数据,添加到对应帖子的评论列表
        if (!empty($fetch['commentid'])) {
            $posts[$postId]['comments'][] = [
                'username' => $fetch['comment_username'],
                'comment' => $fetch['comment'],
                'cmntdate' => $fetch['cmntdate']
            ];
        }
    }
    // 转换为索引数组方便前端循环
    return array_values($posts);
}

第二步:修改前端test.php的展示逻辑

现在可以用新函数获取数据,循环展示每个帖子、对应评论和专属评论框:

echo "<div class='posts-container'>";
$posts = $database->getPostsWithComments();
if (!empty($posts)) {
    foreach ($posts as $post) {
        // 展示帖子主体
        echo "<div class='post'>";
        echo "<h3>".$post['post']."</h3>";
        echo "<p class='post-author'>By ".$post['username']."</p>";
        
        // 展示该帖子的评论区
        echo "<div class='comments-section'>";
        if (!empty($post['comments'])) {
            foreach ($post['comments'] as $comment) {
                echo "<p class='comment'>";
                echo "<a href='#'>".$comment['username'].": </a>".$comment['comment'];
                echo "<br><small class='text-muted'>".$comment['cmntdate']."</small>";
                echo "</p>";
            }
        } else {
            echo "<p>No comments yet. Be the first to comment!</p>";
        }
        echo "</div>";
        
        // 展示当前帖子的专属评论输入框
        echo "<form method='post' action='functions.php' class='comment-form'>";
        echo "<div class='form-group'>";
        echo "<textarea class='form-control' name='comment' placeholder='Write Comment'></textarea>";
        // 这里绑定当前帖子的ID,提交时就能对应到正确帖子
        echo "<input type='hidden' name='postid' value='".$post['postid']."'>";
        echo "<input type='hidden' name='comment_sub' value='1'>";
        echo "<input type='submit' value='Add Comment!' class='btn btn-primary'>";
        echo "</div>";
        echo "</form>";
        
        echo "</div><hr>";
    }
} else {
    echo "No Posts yet.";
}
echo "</div>";

额外修复:原有getComment函数的语法错误

原来的函数里字符串拼接少了引号,正确写法应该是:

$fields[] = "<p><a href='#'>".$fetch['username'].": </a>".$fetch['comment']."<br> <small class='text-muted'>".$fetch['cmntdate']."</small> </p> ";

不过现在用了新的getPostsWithComments函数,这个旧函数可以直接弃用啦。

内容的提问来源于stack exchange,提问作者Haydeer Hussien

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 23:32:43