显示所选线程下的所有评论


Display all comments under a selected thread

我正在创建一个带有评论的新闻系统。一切都很好,但是我不知道如何在一个特定的帖子上显示评论。

当前代码只显示表comments

中的第三条注释

这是ID = 3的评论,但所有其他评论都有$ nid = 3,所以这应该都在ID = 3的news_posts下发布。

这些是我的sql数据库表:

TABLE news_posts (
  id INT(11) NOT NULL AUTO_INCREMENT,
  title VARCHAR(70) NOT NULL,
  author VARCHAR(50) NOT NULL,
  post TEXT NOT NULL,
  DATE DATETIME NOT NULL,
  PRIMARY KEY (id)
)

TABLE comments (
  id INT(11) NOT NULL AUTO_INCREMENT,
  nid INT(11) NOT NULL,
  title VARCHAR(70) NOT NULL,
  author VARCHAR(50) NOT NULL,
  comment TEXT NOT NULL,
  DATE DATETIME NOT NULL,
  PRIMARY KEY (id)
)

这是news.php,应该显示所有的帖子和评论

$abfrage = "SELECT id, title, author, post, DATE_FORMAT(date, GET_FORMAT(DATETIME,'ISO')) as sd FROM news_posts ORDER BY id DESC LIMIT $start,$datensaetze_pro_seite";
 $result = $mysqli->query($abfrage)    
while ($row = $result->fetch_assoc()) { 
    $url = 'news/comments.php?id='. $row['id']; 
    echo '<table class="table table-bordered news"> 
           <thead> 
               <tr> 
                   <th colspan="3"># '. $row['id'] .' | '. $row['title'] .'</th>
             </tr> 
        </thead> 
        <tbody> 
               <tr> 
                   <td colspan="3">'. nl2br(htmlspecialchars(preg_replace('~'S{30}~', ''0 ', $row['post']))) .'</td>
                </tr> 
        </tbody> 
        <tfoot> 
               <tr> 
                   <td colspan="3"> 
                    <small>Beitrag von: '. $row['author'] .' | '. $row['sd'] .'</small>
                     <small class="pull-right"><i class="icon-comment"></i> Kommentar: <a class="accordion-toggle" data-toggle="collapse" href="#show_comment'. $row['id'] .'">anzeigen</a> | <a href="'. $url .'">verfassen</a></small>
                 </td> 
               </tr> 
        </tfoot> 
    </table>'; 
    $abfrage2 = "SELECT id AS comment_id, title AS comment_title, author AS comment_author, comment AS comment_text, DATE_FORMAT(DATE, GET_FORMAT(DATETIME,'ISO')) AS comment_date FROM comments WHERE nid = ". $row['id'] ." ORDER BY id DESC";
    $comment_stmt = $mysqli->prepare($abfrage2) 
    $comment_stmt->execute(); 
    $comment_stmt->bind_result($comment_id, $comment_title, $comment_author, $comment_text, $comment_date))
    while ($comment_stmt->fetch()) { 
        if($comment_stmt->errno) { 
            die($comment_stmt->error); 
        } 
        echo '<div id="show_comment'. $row['id'] .'" class="comment collapse pagination-centered">
             <h4>Kommentare zu</h4> 
            <p>'. $row['title'] .'</p> 
            <div> 
                <table class="table table-bordered news"> 
                       <thead> 
                        <tr> 
                               <th colspan="3">'. $comment_title .'</th> 
                        </tr> 
                    </thead> 
                    <tbody> 
                        <tr> 
                                 <td colspan="3">'. nl2br(htmlspecialchars(preg_replace('~'S{30}~', ''0 ', $comment_text))) .'</td>
                         </tr> 
                    </tbody> 
                    <tfoot> 
                        <tr> 
                            <td colspan="3"> 
                            <small>Beitrag von: '. $comment_author .' | '. $comment_date .'</small>
                             </td> 
                        </tr> 
                    </tfoot> 
                </table> 
            </div> 
        </div>'; 
    } 
    $total_comm = $comment_stmt->num_rows;
    if($total_comm == 0) { 
        echo '<div id="show_comment'. $row['id'] .'" class="comment collapse pagination-centered">
             <h4>Kommentare</h4> 
            <div class="alert alert-info">Es wurden noch keine Kommentare zu diesem Thema verfasst</div>
         </div>'; 
    } 
} 
$comment_stmt->close();

我现在试了好几天运气,但我找不到解决问题的办法....我希望任何人都能帮助我。

修改如下:

 while ($comment_stmt->fetch()) { 

:

 while ($commentRow = $comment_stmt->fetch_assoc()) {