1

目前我有这个结构

<div class="col">
   img1
   img2
   img3
   img4

</div>

<div class="col">
   img1
   img2
   img3
   img4

</div>

然后当我试图循环它时

我得到了这个结果

图像[0] 图像[1] 图像[2]

我怎样才能实现这种结构

<div class ="col"[0]>
    img[0]
    img[1]
    img[2]
    img[3]
</div>
<div class ="col"[1]>
    img[0]
    img[1]
    img[2]
    img[3]
</div>

循环代码:

<?php 
foreach ($images as $k => $v){
    $imagesrc ='graph.facebook.com/'.$v['fbid'].'/picture';; 
?> 

<div class="col" >
    <img [<?php echo $k; ?>] src="<?php echo $imagesrc; ?>" width="29px" height="31px" alt="name">
</div> 

<?php 
} 
?>
4

1 回答 1

2

在循环中,您要在每个图像之后添加一个<br>或。<p>

循环内示例

$counter = 0;
$firstTime = true;
$column = 0;

 <?php foreach ($images as $k => $v): 
  $imagesrc ='graph.facebook.com/'.$v['fbid'].'/picture'; 

  if ($firstTime) {
     echo '<div class="col'.$column.'" >';
     $firstTime = false;
  }

  if ($counter > 3) {
    echo '</div>';
    $column++;
    echo '<div class="col'.$column.'" >';
    $counter = 0;
  } ?>

  <img src="<?php echo $imagesrc; ?>" width="29px" height="31px" alt="name"> 
  <?php $counter++; ?>
 <?php endforeach; ?>
</div>
于 2012-11-29T16:18:52.820 回答