2016-04-19 16 views
0

私はIDに基づいてカテゴリに結果を表示しようとしています。データベースから情報を取得しますが、カテゴリのヘッダーが再び作成されます。例えばグループ化データベースからの結果

タイトルカテゴリのデモ猫1
トピック1匹の猫1

タイトルカテゴリのデモ猫1
トピック2猫1

タイトルカテゴリのデモ猫2
トピック3 cat 2

た場合、それが表示されるはずです:

タイトルカテゴリのデモ猫1
トピック1匹の猫1

トピック2猫1

タイトルカテゴリのデモ猫2
トピック3猫2

<table class="table table-bordered table-hover"> 
    <?php 

$sql = "SELECT * FROM forums, forum_categories WHERE forum_categories.cat_id = forums.cat_id ORDER BY forum_categories.cat_id"; 
$result = query($sql); 

while (($row = mysqli_fetch_assoc($result)) != false) { 

    $cat_id = $row['cat_id'];  
    $cat_title = $row['cat_title']; 

    echo "<thead>"; 
      echo "<tr>"; 
       echo "<th colspan='4'>$cat_title</th>"; 
      "</tr>"; 
     echo "</thead>"; 
     echo "<tbody>"; 


    $forums_cat_id = $row['cat_id']; 
    $forum_name = $row['forum_name']; 
    $forum_desc = $row['forum_desc']; 
    $forum_last_post_id = $row['forum_last_post_id']; 

    echo "<tr>"; 

    echo "<td>$forums_cat_id</td>"; 
    echo "<td>$forum_name<br>$forum_desc<br>Admin</td>"; 
    echo "<td>0</td>"; 
    echo "<td>0</td>"; 
    echo "</tr>"; 
} 

?> 
    </tbody> 
</table> 
+0

あなたがすでに出力しているヘッダーを追跡し、変更時にのみ新しいヘッダーを出力します。例えば'$ prev = null; while(...){if($ prev!= $ cur){出力する$ cur; $ prev = $ cur; }} ' –

+0

Marc B助けてくれてありがとう私はかなり新しいPHPです。私にそれを説明できますか? –

答えて

0

各行に対して、MySQLが返す各結果のヘッダーを印刷しています。したがって、「新しい」カテゴリーが見つかるたびに区別しません。これは、配列のような一時変数を作成することでかなり簡単に行うことができます。たとえば:

$displayedCategories = array(); 

while (($row = mysqli_fetch_assoc($result)) != false) { 
    if (!in_array($row['cat_id'], $displayedCategories)) { 
     // This is a new category, display it and save it in $displayedCategories 
     $displayedCategories[] = $row['cat_id']; 
     echo "<thead>"; 
     echo "<tr>"; 
     echo "<th colspan='4'>$cat_title</th>"; 
     echo "</tr>"; 
     echo "</thead>"; 
    } 

    // Do the other stuff here... 
} 

この方法では、遭遇していない任意の「新しい」カテゴリには、ヘッダが一度に印刷され、カテゴリIDは$displayedCategories配列に保存されます。同じカテゴリーに再度遭遇した場合、ヘッダーは再び出力されません。

+1

Oldskoolコードをポストするのではなく、私に説明するという事実を感謝します。 –

0
<table class="table table-bordered table-hover"><tbody> 
    <?php 

    $sql = "SELECT * FROM forums, forum_categories WHERE forum_categories.cat_id = forums.cat_id ORDER BY forum_categories.cat_id"; 
    $result = query($sql); 
    $cur_cat = ''; 
    while (($row = mysqli_fetch_assoc($result)) != false) { 

     $cat_id = $row['cat_id'];  
     $cat_title = $row['cat_title']; 

     if($cat_title != $cur_cat) { 
      echo "<thead>"; 
       echo "<tr>"; 
        echo "<th colspan='4'>$cat_title</th>"; 
       "</tr>"; 
      echo "</thead>"; 
      $cur_cat = $cat_title; 
     } 


     $forums_cat_id = $row['cat_id']; 
     $forum_name = $row['forum_name']; 
     $forum_desc = $row['forum_desc']; 
     $forum_last_post_id = $row['forum_last_post_id']; 

     echo "<tr>"; 

     echo "<td>$forums_cat_id</td>"; 
     echo "<td>$forum_name<br>$forum_desc<br>Admin</td>"; 
     echo "<td>0</td>"; 
     echo "<td>0</td>"; 
     echo "</tr>"; 
    } 

    ?> 
</tbody></table> 
関連する問題