-3

我的 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=>
4

2 回答 2

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 列的行。我必须采用 count($newarrivaldata)/$numColumns) 的原因是,如果您的数组不是 $numColumns 的精确倍数,您将收到 PHP 的警告,所以我只是想避免这种情况。

于 2012-12-29T10:28:54.577 回答
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++;
  }?>

工作演示

于 2012-12-29T10:29:07.090 回答