-1

我在数据库中有一个表,并在我的 php 文件中获取输出,例如

我的数据库中有一个表“课程”

id  semester   channel             course             coursecode       grade
1   Sem1      Channel1      Digital Networks             C70089          A
2   Sem1      Channel1      Network Theory               N00089          A+
3   Sem1      Channel2      Data & Structur              D70089          B
4   Sem1      Channel2      Object Oriented              C70089          A
5   Sem2      Channel1      Micro Processors             M70089          A
6   Sem2      Channel1      Electricals                  EE0089          B+
7   Sem2      Channel2      Elect & Communs              EC7080          C
8   Sem2      Channel2      Elect Labs                   LEO90           B+

如何通过表格或 div 或 ul 动态显示。谁能建议我做这个设计的简单方法

我认为这是一个愚蠢的问题,因为我尝试了各种方式,我来到了这里

   Sem1                                        Sem2
Channell                                       Channell
     Course       Course Code  Grade        Course            Course Code  Grade
Digital Networks    C70089     A           Micro Processors    -------      ---
Network Theory      N00089     A+          Electricals         -------      ---

Channel2                                       Channel2
Course            Course Code  Grade        Course            Course Code  Grade
Data & Structur     D70089     B           Elect & Communs       -----     -----
Object Oriented     C70089     A           Elect Labs            -----     -----
4

1 回答 1

1

假设您正在使用mysqli_*函数,您的解决方案可能如下所示:

CODEPAD - 为您提供 HTML。
JSFIDDLE - 为您生成的 HTML 设置样式,显示最终结果。

$query = mysql_query('SELECT * FROM course ORDER BY semester, channel');
$semester = $channel = "";
while($row = mysql_fetch_assoc($query))
{
    if ($row["semester"] !== $semester)
    {
        if ($semester !== "")
        {
            echo "    </table>\n";
            echo "</div>\n";
        }

        $semester = $row["semester"];
        $channel = $row["channel"];

        echo "<div class=\"semester\">\n";
        echo "    <h3>{$semester}</h3>\n";
        echo "    <h3>{$channel}</h3>\n";
        echo "    <table>\n";
        echo "        <tr>\n";
        echo "            <th>Course</th>\n";
        echo "            <th>Course Code</th>\n";
        echo "            <th>Grade</th>\n";
        echo "        </tr>\n";
    }

    if ($row["channel"] !== $channel)
    {
        $channel = $row["channel"];

        echo "    </table>\n";
        echo "    <br />\n";
        echo "    <h3>{$channel}</h3>\n";
        echo "    <table>\n";
        echo "        <tr>\n";
        echo "            <th>Course</th>\n";
        echo "            <th>Course Code</th>\n";
        echo "            <th>Grade</th>\n";
        echo "        </tr>\n";
    }

    echo "        <tr>\n";
    echo "            <td>{$row["course"]}</td>\n";
    echo "            <td>{$row["coursecode"]}</td>\n";
    echo "            <td>{$row["grade"]}</td>\n";
    echo "        </tr>\n";
}

if ($semester !== "")
{
    echo "    </table>\n";
    echo "</div>\n";
}
于 2012-09-27T17:50:52.883 回答