foreach循环php中的For循环
我正在codeigniter中成功地获取学生姓名和主题(生成pdf)。 现在我想获取学生记录以获得如下结果:foreach循环php中的For循环,php,arrays,codeigniter,Php,Arrays,Codeigniter,我正在codeigniter中成功地获取学生姓名和主题(生成pdf)。 现在我想获取学生记录以获得如下结果: Student name Drawing Math Computer A 89 66 92 B 65 72 83 C 62 71 86 D 78 73 8
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 } ?>
现在我想水平提取标记,我该怎么做?您可以使用这个
<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>
请试试这个
<?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 } ?>
有几种方法。 直觉上我会做一个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>
大学生
口头的
书写
输出将是:
您可以试试这个,别忘了验证可能出现的空情况
$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>";
$studentrecord=阵列(
0=>数组
(
“口头”=>30,
“书面”=>20,
“图纸”=>10,
“倾听”=>70
),
1=>数组
(
“口头”=>20,
“书面”=>60,
“图纸”=>10,
“倾听”=>40
)
);
回声“;
foreach($studentrecord作为$rec)
{
加入($rec,“”);
}
回声“;
如果您能提出您的问题,那就太好了!另外,不要害怕使用问号(“?”),我想你的意思是说你需要按学生姓名ASC排序。然后在CI中,您可以使用$this->db->order\u by(“学生姓名”、“asc”)代码>