簡體   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