PHP数组图像在div中包含五个图像

时间:2012-12-29 10:16:03

标签: php loops foreach

我的mysql数据库输出了一个图像名称数组,我被困在这个循环中,请他

  <?php foreach ($newarrivaldata as $row){ ?>
       <?php echo $row['imgfile'].'<br/>';?>
  <?php }?>

$ newarrivaldata有18个图像文件 像这样显示它们?

<div id='page1'>    img1    img2    img3    img4    img5    </div>

<div id='page2'>    img6    img7    img8    img9    img10    </div>

<div id='page3'>    img11    img12    img13    img14    img15    </div>

<div id='page4'>    img16    img17    img18                    </div>

更新:var_dump($ newarrivaldata)

array
  0 => 
    array
      'idvehicle' => string '970' (length=3)
      'name' => string 'Toyota RX400' (length=12)
      'imgfile' => string '12122809355412.jpg' (length=18)
      'imgtype' => string 'coverimg' (length=8)
  1 => 
    array
      'idvehicle' => string '972' (length=3)
      'name' => string 'asd' (length=3)
      'imgfile' => string '12122815555612.jpg' (length=18)
      'imgtype' => string 'coverimg' (length=8)

  ....upto 17=>

2 个答案:

答案 0 :(得分:1)

您可以更新您的问题以包含

的输出
print_r($newarrivaldata);

如果你只是想逐字地输出表格(让我建议你重置一切以i = 0,行和列开头,我们不是从1开始,我们总是从0开始,所以page0,img0,等):

$numColumns = 5;
for($row=0; $row<ceil(count($newarrivaldata)/$numColumns); $row++){
   echo "\n<div id='page$row'>";
   for($c=0; $c<$numColumns; $c++){
      if(isset($newarrivaldata[$row+$col])){
         echo '<img src="'.$newarrivaldata[$row+$col]['imgfile'].'" />';
      }
   }
   echo "</div>"
}

我所做的是将您的数组拆分为5行,但将$ numColumns更改为另一个值会将其拆分为$ numColumns列的行。我必须计数($ newarrivaldata)/ $ numColumns)的原因是,如果你的数组不是$ numColumns的确切倍数,你将收到来自PHP的警告,所以我只是想避免这种情况。

答案 1 :(得分:0)

尝试这种方式,这将是一个起点

<?php
$newarrivaldata = array("one", "two", "three", "four","five","six","seven","eight","nine","ten","elevn","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighten"); 
 $key=1;
foreach ($newarrivaldata as $row){ 
      echo $row."&nbsp;&nbsp;";
     if($key%5 == 0) echo "<br/>";
    $key++;
  }?>

Working demo