循环div中的图像并循环div后将包含4个图像

时间:2012-11-29 16:05:55

标签: php codeigniter

目前我有这个结构

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

</div>

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

</div>

然后当我试图循环它

我得到了这个结果

         IMG [0]             IMG [1]             IMG [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 
} 
?>

1 个答案:

答案 0 :(得分:1)

在循环中,您希望在每张图片后添加<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>