[英]For loop inside foreach loop php
I am successfully fetching students name and subject dynamically ( generate pdf ) in codeigniter.我在 codeigniter 中成功地动态获取学生姓名和主题(生成 pdf )。 Now I want to fetch student record to get result like the following:
现在我想获取学生记录以获得如下结果:
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
I am getting student name vertically, now I want student name horizontally like above array, current array is我垂直获取学生姓名,现在我希望学生姓名像上面的数组一样水平,当前数组是
Array
(
[0] => Array
(
[oral] => 30
[Written] => 20
[Drawing] => 10
[Listening] => 70
)
[1] => Array
(
[oral] => 20
[Written] => 60
[Drawing] => 10
[Listening] => 40
)
...
)
Here is the code that generates view, where student record is shown vertically:这是生成视图的代码,其中学生记录垂直显示:
<?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 } ?>
Now I want to fetch marks horizonaly, how do I do that?现在我想水平获取标记,我该怎么做?
You can use this.你可以用这个。
<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>
There is several way of doing it.有几种方法可以做到。 Intuitively I would do a 2x loops.
直觉上我会做一个 2x 循环。
<?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>
You can try this, do not forget to validate the possible empty situations你可以试试这个,不要忘记验证可能的空情况
$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>";
Plz try this请试试这个
<?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 } ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.