PHP For循环,打印特定时间

时间:2014-11-26 06:53:32

标签: php

我想循环和回显图像特定值,我有一个db值,例如100,但我想只回显96个图像不超过那个。此外,DB和循环的值应该打印不超过96(固定)的确切时间

$check = "SELECT white FROM balloons";
$rs = mysqli_query($con,$check);
if(mysqli_num_rows($rs)==1)
{
$row = mysqli_fetch_assoc($rs);
$g=$row['white']; //eg 2,

for($imagecount=0;$imagecount>$g;$imagecount++) {
echo '<img src="img/whiteB.png" class="w_over" />';
}
}

4 个答案:

答案 0 :(得分:2)

for ($i=0; $i < min($g, 96); $i++) {
  echo '<img src="img/whiteB.png" class="w_over" />';
}

答案 1 :(得分:1)

如果我正确地提出了您的问题,您只需更改行,

$g=$row['white'];

为:

$g = ($row['white'] < 96) ? $row['white'] : 96;

旁注

 for($imagecount = 0; $imagecount < $g; $imagecount++) //Should be less then
                                  ^

答案 2 :(得分:1)

我要改变

for($imagecount=0;$imagecount>$g;$imagecount++) {

for($imagecount=0;$imagecount<=min($g,96);$imagecount++) {

PLS。请注意,第二个参数需要反转(循环条件为真)

答案 3 :(得分:0)

for循环条件出错!

更改循环头:

for($imagecount=0;$imagecount<$g;$imagecount++)