简体   繁体   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?

The data is supposed to be dynamic and loaded with an AJAX request.数据应该是动态的并加载了 AJAX 请求。 I have comments, which are being queried just below.我有评论,正在下面查询。 That works no problem.那没问题。 The issue im running into here is trying to query the replies table that correspond with each comment.我在这里遇到的问题是尝试查询与每个评论对应的回复表。 My thinking was to try and put a query inside of the div so that for each COMMENT query, it would query REPLIES for all of its results, then append them automatically for the JSON echo.我的想法是尝试在 div 中放置一个查询,以便对于每个 COMMENT 查询,它会查询所有结果的 REPLIES,然后为 JSON echo 自动附加它们。 But I got an error.但我有一个错误。 I am perplexed at this point as to combine them in a way to get a proper hierarchy of comments/replies.在这一点上,我很困惑以某种方式将它们组合起来以获得适当的评论/回复层次结构。 Is there a way to get a query inside of the div?有没有办法在 div 中获取查询? Or am I going about this completely incorrect?还是我完全不正确? Any help would be much appreciated, been stuck on this for hours.任何帮助将不胜感激,坚持了几个小时。 For simplicity I removed much of the data that is in the divs.为简单起见,我删除了 div 中的大部分数据。

COMMENTS QUERY评论查询

    $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 + divs that need to be appended 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>';
}

Here you go:干得好:
You have to put the replies query inside the comments query so that for every comment query, it queries for each reply.您必须将回复查询放在评论查询中,以便对于每个评论查询,它都会查询每个回复。 You must $stmt->store_result();你必须$stmt->store_result(); the comment query so it can be used in the final results of the output.评论查询,以便它可以用于输出的最终结果。
Next you must use the $comments .= '' with the comments HTML inside the '' to append the comment-block result to the original $comments = '';接下来,您必须使用$comments .= ''里面的评论HTML ''要追加comment-block结果原来的$comments = ''; . . REMOVE the last closing </div> from the comment block, we will use that later.从注释块中删除最后一个结束符</div> ,我们稍后会用到它。 After the while ($stmt2->fetch()) loop you must again use the $comments .= '' with the reply-block HTML inside so that each loop is appended to the comment-block .while ($stmt2->fetch())循环之后,您必须再次使用$comments .= ''reply-block HTML,以便每个循环都附加到comment-block Lastly, outside of the replies loop, you must use the $comments .= '</div>' which closes the block and seals the comment block with its replies within it.最后,在回复循环之外,您必须使用$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