2013-01-13 95 views
1

我想在while循環中顯示一個表..但我已經卡住了2天。任何人都可以幫助我做到這一點?在數據庫值的foreach循環中顯示一個表

現在我將解釋實際上我在這裏要做的事情。在我的數據庫中有幾個類別和幾個主題。每個類別都有自己的主題。現在我需要顯示類別及其主題作爲列表。當在特定的類別顯示科目我需要添加一個HTML表格2列,顯示對象..

這是我迄今所做的代碼..

$categoryIds = implode(',', $_SESSION['category']); 

    $q = "SELECT c. category_id AS ci, c.category_name AS cn, s.subject_name AS sn, s.subject_id AS si 
      FROM category AS c 
      INNER JOIN category_subjects AS cs ON cs.category_id = c.category_id 
      INNER JOIN subjects AS s ON s.subject_id = cs.subject_id 
      WHERE c.category_id IN ($categoryIds)"; 

    $r = mysqli_query($dbc, $q) ; 

    $catID = false; 
    $max_columns = 2; 

    while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) 
    { 
     $categoryId = $row['ci']; 
     $category = $row['cn']; 
     $subjects = $row['sn']; 

     echo '<div>'; 

     //Detect change in category 
     if($catID != $categoryId) 
     { 

      echo "<h3>Category 01: <span>{$category}</span><span></span></h3>\n"; 

      foreach ($subjects AS $sub) { 

       echo "<div class='container'>\n"; 
       //echo "<table><tr>\n"; 

       //echo $sub; 

       echo "</div> <!-- End .container DIV -->\n"; 

      }   
      echo '</div>';   
     }    
     $catID = $categoryId;  
     echo '</div>'; 
    } 

這裏,類別名稱正確顯示在標籤下。但問題是何時要顯示屬於類別的主題。我試圖在.container Div中顯示科目表。

請問有沒有人可以幫助我們解決這個問題......?

謝謝...

回答

1

試着改變你用於檢索數據庫中的數據的方法

... 
//Detect change in category 
if($catID != $categoryId) 
{ 
    echo "<h3>Category 01: <span>{$category}</span><span></span></h3>"; 
    echo "<div class='container'>"; 
    echo "<table>"; 

    if (is_array($subjects)) 
    { 
     foreach ($subjects as $sub) { 
      echo "<tr>"; 
      echo "<td>"; 
      echo $sub; 
      echo "</td>"; 
      echo "</tr>"; 
     } 
    } 
    else 
    { 
     echo "<tr><td>No subjects to display...<td/><tr/>"; 
    } 
    echo "</table>"; 
    echo "</div> <!-- End .container DIV -->"; 
} 
... 

更新

思想。試試這個代碼(代碼沒有測試,打字使用記事本的話),所以你可能需要修正它有點...)

$categoryIds = implode(',', $_SESSION['category']); 

$q = "SELECT c. category_id AS ci, c.category_name AS cn 
     FROM category AS c 
     WHERE c.category_id IN ($categoryIds)"; 

$r = mysqli_query($dbc, $q) ; 

$catID = false; 
$max_columns = 2; 

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) 
{ 
    $categoryId = $row['ci']; 
    $category = $row['cn']; 

    echo '<div>'; 

    echo "<h3>Category 01: <span>{$category}</span><span></span></h3>\n"; 

    $qs = "SELECT s.subject_name AS sn, s.subject_id AS si 
       FROM category_subjects cs 
       INNER JOIN subjects AS s ON s.subject_id = cs.subject_id 
       WHERE cs.category_id = \'' . $categoryId . '\'"; 

    $rs = mysqli_query($dbc, $qs) ; 

    echo "<h3>Category 01: <span>{$category}</span><span></span></h3>"; 
    echo "<div class='container'>"; 
    echo "<table>"; 

    while ($rows = mysqli_fetch_array($rs, MYSQLI_ASSOC)) 
    { 
     $sub = $rows['sn']; 

     echo "<tr>"; 
     echo "<td>"; 
     echo $sub; 
     echo "</td>"; 
     echo "</tr>"; 
    } 

    echo "</table>"; 
    echo "</div> <!-- End .container DIV -->"; 

    echo '</div>';   

} 

在此我們首先提取關鍵字,請在一個循環打印第一類別名稱和在循環內我們爲當前類別獲取適當的主題,然後打印下一個等等...

+0

然後我可以得到這個錯誤..:爲foreach提供的無效參數() – TNK

+0

我更新了代碼,嘗試一下,檢查輸出是否相同? – Nalaka526

+0

然後腳本將其他部分..別的 \t \t \t { \t \t \t \t echo「​​No subject to display ... 」;「 \t \t \t} – TNK

相關問題