2015-10-06 97 views
0

我目前正在開發討論板。我的問題是,它似乎是它給每個類別的4 ID,然後顯示在4類論壇(那不是很好的解釋,但希望你明白)...PHP MySQL創建討論板

<?php 
    /* CATEGORIES */ 
    $query = "SELECT * FROM bkg_categories"; 
     try { 
      $stmt = $db->prepare($query); 
      $result = $stmt->execute(); 
     } catch(PDOException $e) { 
      $error[] = "An error has occured. Please try again later."; 
     } 
     $categories = $stmt->fetchAll(); 

     /* FORUMS */ 
     foreach($categories as $category) { 
      $catid = $category['category_id']; 

      $query = "SELECT * FROM bkg_forums WHERE category_id = :catid"; 
      $query_params = array(':catid' => $catid); 

      try { 
       $stmt = $db->prepare($query); 
       $result = $stmt->execute($query_params); 
      } catch(PDOException $e) { 
        $error[] = "An error has occured. Please try again later."; 
      } 
      $forums = $stmt->fetchAll(); 

      foreach($forums as $forum) { 

       print $forum['forum_id']; 

      } 

     } 

     ?> 

和html將其全部顯示在頁面上。

<?php foreach($categories as $category): ?> 
      <div><?php echo $category['category_name']; ?><?php echo $category['category_id']; ?></div> 
      <table width="100%"> 
       <tr> 
        <td colspan="2">Forum</td> 
        <td>Lastest Post</td> 
        <td>Topics</td> 
        <td>Posts</td> 
       </tr> 
       <?php foreach($forums as $forum): ?> 
        <tr> 
         <td width="5%"></td> 
         <td><a href="viewforum.php?f=<?php echo $forum['forum_id']; ?>"><?php echo $forum['forum_name']; ?></a></td> 
         <td width="15%"> 
          <!--<?php foreach($posts as $post): ?> 
           <a href="viewtopic.php?f=<?php echo $post['forum_id']; ?>&t=<?php echo $post['topic_id']; ?>#<?php echo $post['post_id']; ?>"><?php echo substr($post['post_subject'], 0, 10); ?></a> 
          <?php endforeach; ?>--> 

         </td> 
         <td width="5%" class="text-center"></td> 
         <td width="5%" class="text-center"></td> 
        </tr> 
       <?php endforeach; ?> 
      </table> 
     <?php endforeach; ?> 

編輯:

我創建了四個類別

一般 發展 遊戲 題外話

,並用4 CATEGORY_ID 1論壇,以及1個論壇,顯示在每個類別。

回答

1

您每次都覆蓋$ forum。

你可以做類似

$forums[$catid] = $stmt->fetchAll();

然後

foreach($forums[$category['category_id']] as $forum):