显示所选线程下的所有注释

时间:2013-01-07 23:12:02

标签: php mysqli

我正在创建一个包含评论的新闻系统。一切正常,但我不知道如何在特定帖子上显示评论。

当前代码仅显示表格评论

中的第3条评论

这是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();

我现在想要运气好几天,但我无法找到解决问题的方法......我希望有人能帮助我。

1 个答案:

答案 0 :(得分:-1)

改变这个:

 while ($comment_stmt->fetch()) { 

到此:

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

并从那里继续你的代码..