2013-01-07 29 views
0

我正在用評論創建一個新聞系統。一切正常,但我不知道如何顯示特定帖子上的評論。從表評論顯示所選線程下的所有評論

當前的代碼只顯示3評論這是一個與ID = 3的評論,但所有其他意見有$ NID = 3,所以這都應該與news_posts下發行ID = 3

這些都是我的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(); 

現在我想了好幾天我的運氣,但我無法找到解決我的問題....我希望有人能幫助我。

+0

我首先想到的是嘗試和運行在命令行的SQL語句或使用phpmyadmin,看看你是否至少從你的查詢字符串中獲得正確的數據。沒有看到數據表格的內容,很難確定。 –

回答

-1

更改此:

while ($comment_stmt->fetch()) { 

這樣:

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

,並從那裏繼續你的代碼..

+0

調用錯誤 - >致命錯誤:調用C:\ wamp \ www \ trier \ news \ news.php中的未定義方法mysqli_stmt :: fetch_assoc()。 – user1956396

+0

對不起,我沒有看到你使用的語句不是mysqli查詢.. – Shehabix

+0

請從評論表中添加你的數據庫轉儲記錄 – Shehabix

相關問題