我成功地在codeigniter中动态获取了学生的姓名和主题(生成pdf)。 现在,我想获取学生记录以获取如下结果:
Student name Drawing Math Computer
A 89 66 92
B 65 72 83
C 62 71 86
D 78 73 83
E 82 91 82
我正在垂直获取学生姓名,现在我想像上面的数组那样水平排列学生姓名,当前数组是
Array
(
[0] => Array
(
[oral] => 30
[Written] => 20
[Drawing] => 10
[Listening] => 70
)
[1] => Array
(
[oral] => 20
[Written] => 60
[Drawing] => 10
[Listening] => 40
)
...
)
以下是生成视图的代码,其中学生记录垂直显示:
<?php foreach ($studentrecord as $rec) {?>
<tr>
<td width="200px"><span><?php echo $rec['StudentName'];?></span></td>
<td><span> <?php ?></span></td>
<td><span> </span></td>
<td><span> </span></td>
<td><span> </span></td>
</tr>
<?php } ?>
现在我要水平获取标记,该怎么办?
答案 0 :(得分:0)
您可以使用它。
<tr>
<?php foreach ($studentrecord as $rec) { ?>
<td width="200px"><span><?php echo $rec['StudentName'];?></span></td>
<?php } ?>
</tr>
<tr>
<?php foreach ($studentrecord as $rec) { ?>
<td><span> <?php echo $rec['oral'];?></span></td>
<?php } ?>
</tr>
<tr>
<?php foreach ($studentrecord as $rec) { ?>
<td><span> <?php echo $rec['oral'];?></span></td>
<?php } ?>
</tr>
<tr>
<?php foreach ($studentrecord as $rec) { ?>
<td><span> <?php echo $rec['oral'];?></span></td>
<?php } ?>
</tr>
<tr>
<?php foreach ($studentrecord as $rec) { ?>
<td><span> <?php echo $rec['oral'];?></span></td>
<?php } ?>
</tr>
答案 1 :(得分:0)
有几种方法可以做到。 凭直觉,我会做2次循环。
<?php
$array = [["oral"=>12,"writen"=>20],["oral"=>13,"writen"=>15],["oral"=>12,"writen"=>18]];
?>
<!DOCTYPE html>
<html>
<body>
<table>
<tr>
<th>student</th>
<th>oral</th>
<th>writen</th>
</tr>
<?php
foreach($array as $key => $value) {
// $key is index of student
// $value is his notes.
echo "<tr>";
echo "<td>".$key."</td>";
// need to do a second loop for each mark
foreach($value as $key2 ) {
echo "<td>";
echo $key2;
echo "<td>";
}
echo "</tr>";
}
?>
</table>
</body>
</html>
输出将是: output
答案 2 :(得分:0)
您可以尝试此操作,不要忘记验证可能的空白情况
$studentrecord = Array(
0 => Array
(
'oral' => 30,
'Written' => 20,
'Drawing' => 10,
'Listening' => 70
),
1 => Array
(
'oral' => 20,
'Written' => 60,
'Drawing' => 10,
'Listening' => 40
)
);
echo "<table border='1'>";
foreach ($studentrecord as $rec)
{
echo "<tr><td>".join($rec,"</td><td>")."</td></tr>";
}
echo "</table>";
答案 3 :(得分:-2)
请尝试一下
<?php foreach ($studentrecord as $key=> $rec) {?>
<tr>
<td width="200px"><span><?php echo $rec[$key]['StudentName'];?></span>
</td>
<td><span> <?php ?></span></td>
<td><span> </span></td>
<td><span> </span></td>
<td><span> </span></td>
</tr>
<?php } ?>