PHP MySQL创建讨论板

问题描述:

我目前正在开发讨论板。我的问题是,它似乎是它给每个类别的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个论坛,显示在每个类别。

您每次都覆盖$ forum。

你可以做类似

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

然后

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