如何使用for循环在div中放置一组数据

时间:2017-05-22 12:21:06

标签: php css arrays

我的数组

   $images = array(
    'images/logo.png',
    'files/product_image/asansam-dongle-asan-sam-dongle-P688075T.jpg',
    'default/img/noimage.png',
    'default/img/default-avatar.png',
    'images/logo.png',
    'files/product_image/asansam-dongle-asan-sam-dongle-P688075T.jpg',
    'default/img/noimage.png',
    'default/img/default-avatar.png',
    'images/logo.png',
    'files/product_image/asansam-dongle-asan-sam-dongle-P688075T.jpg'
   );

@for ($i=0; $i < count($images) ; $i++)
    @if($i%5 == 0)
        <div class="{{$i}}">
    @endif
        <img src="{{URL::to(''.$images[$i])}}">
    @if($i%5 == 0)
        </div>
    @endif
@endfor

这里我需要每5个图像生成一个div ..即

前5个图像应该在一个div中,另一个(下一个)5个图像应该在另一个div中。

现在我得到类似的内容

<div class="0">
    <img src="http://localhost/girija/eshop/public/images/logo.png">
</div>
    <img src="http://localhost/girija/eshop/public/files/product_image/asansam-dongle-asan-sam-dongle-P688075T.jpg">
    <img src="http://localhost/girija/eshop/public/default/img/noimage.png">
    <img src="http://localhost/girija/eshop/public/default/img/default-avatar.png">
    <img src="http://localhost/girija/eshop/public/images/logo.png">
<div class="5">
    <img src="http://localhost/girija/eshop/public/files/product_image/asansam-dongle-asan-sam-dongle-P688075T.jpg">
</div>
    <img src="http://localhost/girija/eshop/public/default/img/noimage.png">
    <img src="http://localhost/girija/eshop/public/default/img/default-avatar.png">
    <img src="http://localhost/girija/eshop/public/images/logo.png">
    <img src="http://localhost/girija/eshop/public/files/product_image/asansam-dongle-asan-sam-dongle-P688075T.jpg">

但我需要这样

<div class="0">
    <img src="http://localhost/girija/eshop/public/images/logo.png">
    <img src="http://localhost/girija/eshop/public/files/product_image/asansam-dongle-asan-sam-dongle-P688075T.jpg">
    <img src="http://localhost/girija/eshop/public/default/img/noimage.png">
    <img src="http://localhost/girija/eshop/public/default/img/default-avatar.png">
    <img src="http://localhost/girija/eshop/public/images/logo.png">
</div>
<div class="5">
    <img src="http://localhost/girija/eshop/public/files/product_image/asansam-dongle-asan-sam-dongle-P688075T.jpg">
    <img src="http://localhost/girija/eshop/public/default/img/noimage.png">
    <img src="http://localhost/girija/eshop/public/default/img/default-avatar.png">
    <img src="http://localhost/girija/eshop/public/images/logo.png">
    <img src="http://localhost/girija/eshop/public/files/product_image/asansam-dongle-asan-sam-dongle-P688075T.jpg">
</div>

我该怎么做?

4 个答案:

答案 0 :(得分:2)

由于$ i从0开始,你可以改变你的状况

@for ($i=0; $i < count($images) ; $i++)
@if(($i+1)%6 == 0)
    <div class="{{$i}}">
@endif
    <img src="{{URL::to(''.$images[$i])}}">
@if(($i+1)%6 == 0)
    </div>
@endif
@endfor

答案 1 :(得分:1)

使用array_chunk()

foreach(array_chunk($images,5) as $sub_images){
    echo "<div>";

    foreach($sub_images as $image){
        echo '<img src="$image"/>';
    }

    echo "</div>";
}

答案 2 :(得分:1)

像这样改变循环: -

$images = array(
    'images/logo.png',
    'files/product_image/asansam-dongle-asan-sam-dongle-P688075T.jpg',
    'default/img/noimage.png',
    'default/img/default-avatar.png',
    'images/logo.png',
    'files/product_image/asansam-dongle-asan-sam-dongle-P688075T.jpg',
    'default/img/noimage.png',
    'default/img/default-avatar.png',
    'images/logo.png',
    'files/product_image/asansam-dongle-asan-sam-dongle-P688075T.jpg'
   );
<div class="0">
@for ($i=1; $i < count($images) ; $i++)
   <img src="{{URL::to(''.$images[$i])}}">
    @if($i%5 == 0)
       </div><div class="{{$i}}">
    @endif
@endfor
</div>

答案 3 :(得分:1)

   $flag = 1;    
   @for ($i=0; $i < count($images) ; $i++)
        @if($flag%5 == 0)
            @if($i == 0)
                <div class="{{$i}}">
              @else:
                 <div class="{{$flag}}"> 
            @endif
        @endif
            <img src="{{URL::to(''.$images[$i])}}">
        @if($flag%5 == 0)
            </div>
        @endif
    @endfor