我有一个数组,我想在多个表中显示数据。特定年份和学期的所有模块应该在一个表中。例如,semester_id
1和{{1}中的所有模块应该在一个表中
year 1
到目前为止,这是我的代码
Array
(
[0] => Array
(
[year_id] => 1
[semester_id] => 1
[module_code] => MATH1103C
[module_name] => Decision Mathematics
[module_credit] => 3
)
[1] => Array
(
[year_id] => 1
[semester_id] => 1
[module_code] => ITE1107C
[module_name] => Language and Communication Seminar
[module_credit] => 3
)
[2] => Array
(
[year_id] => 1
[semester_id] => 1
[module_code] => MGMT1101C
[module_name] => Management Seminar
[module_credit] => 3
)
[3] => Array
(
[year_id] => 1
[semester_id] => 1
[module_code] => HCA1105C
[module_name] => Computer Architecture
[module_credit] => 4
)
[4] => Array
(
[year_id] => 1
[semester_id] => 1
[module_code] => PROG1115C
[module_name] => Object Oriented Software Development I
[module_credit] => 4
)
[5] => Array
(
[year_id] => 1
[semester_id] => 2
[module_code] => WAT1116C
[module_name] => Internet Programming I
[module_credit] => 4
)
[6] => Array
(
[year_id] => 1
[semester_id] => 2
[module_code] => MATH1101C
[module_name] => Analytic Methods for Computing
[module_credit] => 4
)
[7] => Array
(
[year_id] => 1
[semester_id] => 2
[module_code] => PROG1116C
[module_name] => Object Oriented Software Development II
[module_credit] => 4
)
[8] => Array
(
[year_id] => 1
[semester_id] => 2
[module_code] => DBT1111C
[module_name] => Database Design
[module_credit] => 4
)
[9] => Array
(
[year_id] => 2
[semester_id] => 1
[module_code] => PROG2117C
[module_name] => Desktop Application Development
[module_credit] => 4
)
[10] => Array
(
[year_id] => 2
[semester_id] => 1
[module_code] => OSS2109C
[module_name] => Operating Systems
[module_credit] => 4
)
[11] => Array
(
[year_id] => 2
[semester_id] => 1
[module_code] => CAN2112C
[module_name] => Network Design & Programming
[module_credit] => 4
)
[12] => Array
(
[year_id] => 2
[semester_id] => 1
[module_code] => WAT2117C
[module_name] => Internet Programming II
[module_credit] => 4
)
[13] => Array
(
[year_id] => 2
[semester_id] => 2
[module_code] => MGMT2104C
[module_name] => Research & Development Seminar
[module_credit] => 3
)
[14] => Array
(
[year_id] => 2
[semester_id] => 2
[module_code] => MCT2104C
[module_name] => Mobile Application Development
[module_credit] => 4
)
[15] => Array
(
[year_id] => 2
[semester_id] => 2
[module_code] => WAT2124C
[module_name] => Web Services
[module_credit] => 4
)
[16] => Array
(
[year_id] => 2
[semester_id] => 2
[module_code] => MATH2323C
[module_name] => Numerical Methods
[module_credit] => 4
)
[17] => Array
(
[year_id] => 2
[semester_id] => 2
[module_code] => SDT2114C
[module_name] => Requirements Engineering
[module_credit] => 4
)
[18] => Array
(
[year_id] => 3
[semester_id] => 1
[module_code] => SEM3112C
[module_name] => Project Management Seminar
[module_credit] => 3
)
[19] => Array
(
[year_id] => 3
[semester_id] => 1
[module_code] => SECU3119C
[module_name] => Secure Software Development
[module_credit] => 4
)
[20] => Array
(
[year_id] => 3
[semester_id] => 1
[module_code] => MULT3114C
[module_name] => Game Development
[module_credit] => 4
)
[21] => Array
(
[year_id] => 3
[semester_id] => 2
[module_code] => PROJ3105C
[module_name] => Systems Development Project
[module_credit] => 9
)
[22] => Array
(
[year_id] => 3
[semester_id] => 2
[module_code] => MGMT3105C
[module_name] => Entrepreneurship Seminar
[module_credit] => 3
)
[23] => Array
(
[year_id] => 3
[semester_id] => 2
[module_code] => SEM3113C
[module_name] => Software Quality Management
[module_credit] => 4
)
[24] => Array
(
[year_id] => 3
[semester_id] => 2
[module_code] => WAT3125C
[module_name] => Emerging Web Technologies
[module_credit] => 4
)
[25] => Array
(
[year_id] => 3
[semester_id] => 2
[module_code] => SDT3104C
[module_name] => Enterprise Software Development
[module_credit] => 4
)
)
答案 0 :(得分:0)
您需要将静态代码放在for循环之外(即全部)。
您必须仅为来自数据库
的数据生成动态行 ie ({$rows[$i]['module_code']})
无需使用多个头或表只需创建一个表,一个头,然后在tr内使用,然后为数组数据创建动态。
<table>
<thead>
<tr>
<th>Module Code</th>
</tr>
</thead>
<tbody>
<?php
for ($i = 0; $i < count($rows); $i++){
?>
<tr>
<td><?php echo $rows[$i]['module_code'] ?></td>
</tr>
<?php } ?>
</tbody>
</table>
这适用于需要创建单个表的情况。 例如,在semester_id 1和year 1中找到的所有模块都应该在一个表中。在这种情况下,在数组中运行表并在上面放置条件 如果匹配的新表,则继续当前表
答案 1 :(得分:0)
迭代一个简单的查询
SELECT DISTINCT year_id, semester_id FROM tablename;
对于每一行,打印表头并迭代另一个预准备语句的行
SELECT ... FROM yourQuery WHERE year_id = ? AND semester_id = ?;
打印表格底部,结束时
结束时(外部迭代)