【发布时间】:2013-01-11 16:49:32
【问题描述】:
$rows = getChallengeList();
error_log(print_r($rows, 1));
?>
<table border="1">
<tr><th>ID</th><th>Title</th><th>Description</th></tr>
<?php foreach ($rows as $row): ?>
<?php foreach($row as $chal): ?>
<tr>
<td><?= $chal['loyalty_challenges_id']; ?></td>
<td><?= $chal['title']; ?></td>
<td><?= $chal['description']; ?></td>
</tr>
<?php endforeach; ?>
<?php endforeach; ?>
</table>
error_log 会返回:
[11-Jan-2013 10:44:27] Array
(
[0] => Array
(
[loyalty_challenges_id] => 1
[title] => New Customer Special
[description] => Reward new customers with a free order of breadsticks after placing their second order during their first 30 days
)
[1] => Array
(
[loyalty_challenges_id] => 2
[title] => Frequent Flyer Special
[description] => Reward long-time customers who order often with a free pizza
)
)
但是循环渲染的值是这样的:
<table border="1">
<tr><th>ID</th><th>Title</th><th>Description</th></tr>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>N</td>
<td>N</td>
<td>N</td>
</tr>
<tr>
<td>R</td>
<td>R</td>
<td>R</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>F</td>
<td>F</td>
<td>F</td>
</tr>
<tr>
<td>R</td>
<td>R</td>
<td>R</td>
</tr>
</table>
知道是什么原因造成的吗?我已经有一段时间没有使用直接的 php,而不是拥有自己的数组处理功能的 CMS。
【问题讨论】:
-
请停止使用短标签。
-
您只需要一个
foreach。 -
@njk - 有人告诉我,如果您要使用大量带有 php 值的 HTML,最好这样做,而不是回显数百行 html,这样您就可以留在里面一个 php 标签。
-
@EmmyS 看看这个:Are PHP short tags acceptable to use?
-
@Asok - 根据那个答案,“正如 ThiefMaster 在 cmets 中提到的那样,从 PHP 5.4 开始,= ... ?> 标签在任何地方都受支持,无论短标签设置如何。这应该意味着它们在可移植代码中使用是安全的,但这确实意味着对 PHP 5.4+ 的依赖”。不是问题 - 我们拥有所有服务器,因此我们可以完全控制它们。