【发布时间】:2018-06-08 11:05:32
【问题描述】:
我编写了从数组变量写入表的代码,但它显示了重复的列:
这里是数组值:
$data = [
[
"title" => "The World's End",
"genre" => "Sci-fi",
"year" => 2013,
"gross" => 21
],
[
"title" => "Scott Pilgrim vs. the World",
"genre" => "Sadness",
"year" => 2010,
"gross" => 21
],
[
"title" => "Hot Fuzz",
"genre" => "Buddy Cop",
"year" => 2007,
"gross" => 21
],
[
"title" => "Shaun of the Dead",
"genre" => "Zombie",
"year" => 2007,
"gross" => 21
],
];
和表写代码:
<table>
<tr>
<?php
foreach ($data as $item){
foreach ($item as $key => $drop){
echo '<th>' . $key . '</th>';
}
}
?>
</tr>
<?php foreach ($data as $item) : ?>
<tr>
<td><?= $item['title']; ?></td>
<td><?= $item['genre']; ?></td>
<td><?= $item['year']; ?></td>
<td>€ <?= $item['gross']; ?></td>
<?php endforeach; ?>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>€
<?php
$gross_sum = 0;
foreach ($data as $gross_value)
{
$gross_sum += $gross_value['gross'];
}
echo $gross_sum;
?>
</td>
</tr>
</table>
如何从我的代码内容中删除表格中重复的右侧列?
【问题讨论】:
-
为什么要以这种方式从数据中生成表头?我要么手动完成,要么拥有一个多维数组,其中一个键用于标题,一个用于数据
标签: php html-table multiple-columns