【发布时间】:2013-04-05 18:43:18
【问题描述】:
我正在编写一个脚本来从数据库中提取并动态创建一个项目表。每次我尝试增加变量时,我使用的循环都会中断。
下面是一个结果相同的例子:
这个循环可以很好地创建多个表。
<?php
$item=array("item1", "item2", "item3", "item4", "item5", "item6", "item7");
$i=0;
while($i!=count($item)){
$galleryItem.=<<<HTML
<table>
<tr>
<td>$item[$i]</td>
</tr>
</table>
HTML;
$i++;
}
echo $galleryItem;
?>
但是,此循环将不起作用。我希望它在表中创建两列,并在多行中完整输出数组。
<?php
$item=array("item1", "item2", "item3", "item4", "item5", "item6", "item7");
$i=0;
while($i!=count($item)){
$galleryItem.=<<<HTML
<table>
<tr>
<td>$item[$i]</td>
HTML;
$i++;
$galleryItem.=<<<HTML
<td>$item[$i]</td>
</tr>
</table>
$i++;
}
echo $galleryItem;
?>
我可能做错了什么? PHP 是否不允许您在 while 循环中多次写入同一个变量?
【问题讨论】:
-
你没有关闭最后一个HEREDOC
-
谢谢,但代码仍然无法运行
-
你也可以通过 foreach 循环来做到这一点
-
您希望输出如何?请编辑问题并显示。
标签: php arrays while-loop increment heredoc