0

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

当前代码仅显示表格注释中的第三条注释

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

我现在正在尝试几天的运气,但我找不到解决我的问题的方法......我希望有人能帮助我。

4

1 回答 1

-1

改变这个:

 while ($comment_stmt->fetch()) { 

对此:

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

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

于 2013-01-07T23:19:27.667 回答