繁体   English   中英

如何查询每一行的 COMMENTS 表,然后查询 REPLIES TABLE 并在 JSON echo 之前将每个结果附加到 COMMENTS 中?

[英]How to query COMMENTS table for each row, then query REPLIES TABLE and append each result inside COMMENTS before JSON echo?

数据应该是动态的并加载了 AJAX 请求。 我有评论,正在下面查询。 那没问题。 我在这里遇到的问题是尝试查询与每个评论对应的回复表。 我的想法是尝试在 div 中放置一个查询,以便对于每个 COMMENT 查询,它会查询所有结果的 REPLIES,然后为 JSON echo 自动附加它们。 但我有一个错误。 在这一点上,我很困惑以某种方式将它们组合起来以获得适当的评论/回复层次结构。 有没有办法在 div 中获取查询? 还是我完全不正确? 任何帮助将不胜感激,坚持了几个小时。 为简单起见,我删除了 div 中的大部分数据。

评论查询

    $sql="SELECT c.cid, DATE_FORMAT(c.posttime, '%a, %b %D | %l:%i %p'), c.comment,
    p.profilepic,
    u.fname, u.lname
    from taskcomments c
    inner join users u on u.uid = c.uid
    inner join profile p on c.uid = p.uid
    where c.pid = ?";
$stmt3 = $conn->prepare($sql);
$stmt3->bind_param("i", $_POST['cpid']);
$stmt3->execute();
$stmt3->bind_result($cid,$posttime,$comment,$profilepic,$fname,$lname);
$comments = ''; 
while($stmt3->fetch()){ 
$comments .= '<div class="comment-block" id="'.$cid.'">
                    <div class="comment-all-container">
                        <div class="commenter-info-content">                                  
                        </div>
                    </div>
                     //need each additional query to be appended here before echo
                </div>';
$output=array(
        'comments' => $comments
    );

}
$json=json_encode($output);
echo $json;

REPLIES QUERY + 需要追加的 div

$sql="SELECT r.rid, DATE_FORMAT(r.replydate, '%a, %b %D | %l:%i %p'), r.reply,
    p.profilepic,
    u.fname, u.lname
    from taskcommentreplies r
    inner join users u on u.uid = r.uid
    inner join profile p on r.uid = p.uid
    where r.cid = ?";
$stmt4 = $conn->prepare($sql);
$stmt4->bind_param("i", $cid);
$stmt4->execute();
$stmt4->bind_result($rid,$replydate,$reply,$profilepic,$fname,$lname);
$replies = ''; 
while($stmt4->fetch()){

$replies .= '<div class="reply-block" id="'.$rid.'">
                   <div class="commenter-picture">
                       <img class="commenter-photo" src="/'.$profilepic.'">
                    </div>
                    <div class="commenter-info-content">
                    </div>
                </div>';
}

干得好:
您必须将回复查询放在评论查询中,以便对于每个评论查询,它都会查询每个回复。 你必须$stmt->store_result(); 评论查询,以便它可以用于输出的最终结果。
接下来,您必须使用$comments .= ''里面的评论HTML ''要追加comment-block结果原来的$comments = ''; . 从注释块中删除最后一个结束符</div> ,我们稍后会用到它。 while ($stmt2->fetch())循环之后,您必须再次使用$comments .= ''reply-block HTML,以便每个循环都附加到comment-block 最后,在回复循环之外,您必须使用$comments .= '</div>'它关闭块并将评论块与其中的回复密封在一起。

    $sql="SELECT
    c.cid, DATE_FORMAT(c.posttime, '%a, %b %D | %l:%i %p'), c.comment,
    p.profilepic,
    u.fname, u.lname
    from taskcomments c
    left join users u on u.uid = c.uid
    left join profile p on c.uid = p.uid
    where c.pid =?
    ORDER BY c.posttime ASC";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $_POST['cpid']);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($ccid,$cposttime,$ccomment,$cprofilepic,$cfname,$clname);
$comments = ''; 
while($stmt->fetch()){   
$comments .= '
<div class="comment-block" id="'.$ccid.'">
    <div class="comment-all-container">
       <div class="commenter-picture">
           <img class="commenter-photo" src="/'.$cprofilepic.'">
        </div>
        <div class="commenter-info-content">
            <div class="commenter-info">
                 <div class="commenter-name">
                      '.$cfname.' '.$clname.'
                  </div>
                  <div class="comment-time">
                        <p>'.$cposttime.'</p>
                  </div>
             </div>
             <div class="comment-data-container">
                   <div class="comment-data">
                        <p>'.$ccomment.'</p>
                    </div>
                     <div class="reply-reply-container">
                           <a class="reply-tag" onclick="reply(this)">Reply</a>
                    </div>
              </div>                                   
          </div>
    </div>   
';
$sql2="SELECT
    r.rid, DATE_FORMAT(r.replydate, '%a, %b %D | %l:%i %p'), r.reply,
    p.profilepic,
    u.fname, u.lname
    from taskcommentreplies r
    left join users u on u.uid = r.uid
    left join profile p on r.uid = p.uid
    WHERE r.cid=?
    ORDER BY r.replydate ASC";
$stmt2 = $conn->prepare($sql2);
$stmt2->bind_param('i', $ccid);    
$stmt2->execute();
$stmt2->bind_result($rrid,$rreplydate,$rreply,$rprofilepic,$rfname,$rlname);
while ($stmt2->fetch()){
$comments .= '<div class="reply-block" id="'.$rrid.'">
                   <div class="commenter-picture">
                       <img class="commenter-photo" src="/'.$rprofilepic.'">
                    </div>
                    <div class="commenter-info-content">
                        <div class="commenter-info">
                             <div class="commenter-name">
                                  '.$rfname.' '.$rlname.'
                              </div>
                              <div class="comment-time">
                                    <p>'.$rreplydate.'</p>
                              </div>
                         </div>
                         <div class="comment-data-container">
                               <div class="comment-data">
                                    <p>'.$rreply.'</p>
                                </div>
                                 <div class="reply-reply-container">
                                       <a class="reply-tag" onclick="reply(this)">Reply</a>
                                </div>
                          </div>                                   
                      </div>
                </div>
            ';

}
$comments .= '</div>';    
}
$output=array(
    'comments' => $comments
    );

$json=json_encode($output);
echo $json;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM