在数据库值的foreach循环中显示一个表 [英] Display a table in a foreach loop with database values

查看:88
本文介绍了在数据库值的foreach循环中显示一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在while循环中显示一张表格,但我在那里呆了两天。任何人都可以帮助我做到这一点?

现在我将解释我正在尝试在这里做什么..在我的数据库中有几个类别和几个主题。每个类别都有自己的主题。现在我需要显示类别及其主题作为列表。当在特定类别下显示主题时,我需要添加一个包含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主体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>';

//如果($ catID!= $ categoryId)
{

echo< h3>类别01:< ;跨度> {$类别}< /跨度><跨度>< /跨度>< / H3> \\\
;

foreach($ subject AS $ sub){

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

// echo $ sub;

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

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

这里,Category标签正确显示在标签下。但问题是何时要显示属于类别的主题。我试图在.container Div中显示科目表。

请问有谁能帮我们解决这个问题......?



谢谢...

解决方案



  ... 
//检测类别
中的变化if($ catID!= $ categoryId)
{
echo< h3>类别01:< span> {$ category}< / span>< span>< / span>< / h3>;
echo< div class ='container'>;
回显< table>;

if(is_array($ subjects))
{
foreach($ subject as $ sub){
echo< tr>;
回显< td>;
echo $ sub;
回声< / td>;
回声< / tr>;
}
}
else
{
echo< tr>< td>没有要显示的主题...< td />< tr />中;
}
echo< / table>;
echo< / div><! - End .container DIV - >;
}
...






< h1>更新

改变您用来从数据库检索数据的方法。试试这段代码(代码没有经过测试,用记事本输入),所以你可能需要修改一下...)

  $ 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>';
$ b $回声< h3>类别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主题AS s ON s.subject_id = cs.subject_id
WHERE cs.category_id = \''。$ categoryId。'\';

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

echo< h3>类别01:< span> {$ category}< / span>< span>< / span>< / h3>;
echo< div class ='container'>;
回显< table>;

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

echo< tr>;
回显< td>;
echo $ sub;
回声< / td>;
回声< / tr>;
}

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

echo'< / div>';






在此我们首先获取类别,进入循环打印第一个类别名称,并在循环中为当前类别获取适当的主题,然后打印下一个等等...


I am trying to display a table in a while loop.. But I have got stuck there for 2 days. anyone can help me to do this?

Now I will explain actually what I am trying to do here.. There are several categories and several subjects in my database. each category have its own subjects. Now I need to display category and its subjects as a list. When display subjects under particular category I need to add a HTML table with 2 columns to display subjects..

This is the code that I have done so far..

    $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>';
   }

Here, Category Name display correctly under tag. But problem is when going to display subjects which are belongs to categories. I am trying to display subjects table in .container Div.

please Is anybody there can I get help whit this issue...?

Thank you...

解决方案

Try

...
//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 -->";
}
...


Update

Thought of changing the approach you've used to retrieve the data from DB. Try this code (code is not tested, typed it using notepad) so you may need to fix it a bit...)

$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>';          

}

In this we fetch Categories first, go in a loop print first Category name and within the loop we fetch appropriate Subjects for the current category, then print the next one and so on...

这篇关于在数据库值的foreach循环中显示一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆