使用数据库值在foreach循环中显示表

时间:2013-01-13 14:40:42

标签: php html mysql

我试图在while循环中显示一个表..但是我已经在那里停留了2天。有人可以帮我这么做吗?

现在我将解释实际上我在这里要做的事情。我的数据库中有几个类别和几个主题。每个类别都有自己的科目。现在我需要将类别及其主题显示为列表。当在特定类别下显示主题时,我需要添加一个包含2列的HTML表来显示主题..

这是我到目前为止所做的代码..

    $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 个答案:

答案 0 :(得分: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>';          

}

在这里我们首先获取Categories,然后在循环中打印第一个Category类别,在循环中我们为当前类别获取适当的Subjects,然后打印下一个,依此类推......