我在数据库中有一个表,并在我的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+
我怎么能用table或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 ----- -----
答案 0 :(得分: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";
}