我正在尝试制作一个PHP照片库(是的,我尝试过其他解决方案,没有一个能满足我想要的设置),我正在尝试让这个while
循环在三次后停止,因为我想每个表行显示三个缩略图。这是我当前的代码:
<table>
<tr>
<?php while($row2 = mysql_fetch_assoc($result)) { echo "
<td><a href='URL/".$row2['file']."'><img src='URL/".$row2['file']."' width='200' height='106' /></a></td>
"; } ?>
</tr>
</table>
我不记得学过任何关于停止PHP循环的知识,所以请帮忙!非常感谢。
所以您实际想要的是每行3个图像。我认为这会起作用:
<table>
<?php
$i = 0;
while($row2 = mysql_fetch_assoc($result)) {
if($i%3==0)
echo "<tr>";
echo "<td><a href='URL/".$row2['file']."'><img src='URL/".$row2['file']."' width='200' height='106' /></a></td>";
if($i%3==2)
echo "</tr>";
$i++;
}
if($i%3!=0)
echo "</tr>";
?>
</table>
<table>
<tr>
<?php
$i=1;
while($row2 = mysql_fetch_assoc($result)) { echo "
<td><a href='URL/".$row2['file']."'><img src='URL/".$row2['file']."' width='200' height='106' /></a></td>
";
if($i>=3) break;
$i++;
} ?>
</tr>
</table>
尝试将AND添加到while并与计数器进行比较而($row2=mysql_fetch_assoc($result)&$i<3)
在循环之前将$i设置为0
为什么不限制原始MySQL查询中返回的结果?您可以这样添加查询限制:
limit 3
这样$result将只有三行,因此不需要额外的PHP,也不需要来自内存中DB的更少数据
您可以添加一个计数器,如:
$nr = 1;
while($row2 = mysql_fetch_assoc($result)) {
if ($nr < 4) {
do your thing;
}
$nr++;
}