2 回答

TA贡献2012条经验 获得超12个赞
只是为了完整起见,当您将 PHP 与 HTML 混合使用时,不需要echo所有内容。
//not sure what is above this, so will just end it here.
?>
<table>
<tr>
<th>FullName</th>
<th>Email</th>
<th>City</th>
<th>State</th>
</tr>
<?php foreach ($people as $person) : ?>
<tr>
<?php foreach ($person as $key => $values) : ?>
<td><?php echo $values; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
创建完整有效的代码。不要遗漏<tr></tr>,以另一种方式解决 CSS 问题。
======
另一种做事方式。代码可能看起来有点复杂,但更容易维护。例如,如果您想添加邮政编码,您所要做的就是更改数组$fields。其他一切都会适应。
$fields = ['name','email','city','state'];
?>
<table>
<tr>
<?php foreach($fields as $field): ?>
<th><?php echo ucfirst($field); ?></th>
<?php endforeach; ?>
</tr>
<?php foreach ($people as $person) : ?>
<tr>
<?php foreach ($fields as $field) : ?>
<td> <?php echo $person[$field]; ?> </td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>

TA贡献1802条经验 获得超5个赞
表行需要在第一个循环中。
echo "<table>";
echo "<th>FullName</th>";
echo "<th>Email</th>";
echo "<th>City</th>";
echo "<th>State</th>";
foreach ($people as $person) {
echo "<tr>";
foreach ($person as $key=>$values) {
echo "<td>$values</td>";
}
echo "</tr>";
}
echo "</table>";
- 2 回答
- 0 关注
- 115 浏览
添加回答
举报