我有以下代码:
for($i=0; $i<count($gallery);$i++)
{
$temp = array();
$temp = $gallery[$i];
echo "<img src='". $temp->path . "' />";
}
现在这段代码将内容打印在一行中。我只想每行打印 3 个,然后创建新行并打印另外 3 个,依此类推。如何才能做到这一点?
感谢您的支持:)
编辑:错误
遇到 PHP 错误
严重性:通知
消息:未定义的偏移量:5
文件名:views/profile.php
线路编号:105
遇到 PHP 错误
严重性:通知
消息:尝试获取非对象的属性
文件名:views/profile.php
线路号码:106
你可以做
$n = 3;
echo "<table><tr>";
for($i=0; $i<count($gallery);$i++){
$temp = array();
$temp = $gallery[$i];
echo "<td><img src='". $temp->path . "' /></td>";
if($i % $n ==0 && $i!=0 ){
echo "</tr><tr>";
}
}
echo '</tr></table>';
Edit:
如果你想以“正确”的方式做到这一点 - 通过构建语法正确的 HTML,你需要这样做:
$n = 3;
echo "<table><tr>";
$gallery_count = count($gallery);
for($i=0; $i<$gallery_count; $i++){
$temp = array();
$temp = $gallery[$i];
echo "<td><img src='". $temp->path . "' /></td>";
if($i != 0){
if($i % $n == 0 && $i != $gallery_count-1){
echo "</tr><tr>";
}
else{
echo ""; //if it is the last in the loop - do not echo
}
}
}
//example - if the last 2 `td`s are missing:
$padding_tds = $gallery_count % $n;
if($padding_tds != 0 ){
$k = 0;
while($k < $padding_tds){
echo "<td> </td>";
}
}
echo '</tr></table>';
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)