Suganya Rajasekar Suganya Rajasekar -4 years ago 77
PHP Question

how to place set of datas in a div using for loop

My Array

$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


Here I need to generate a div each 5 images.. i.e.,

first 5 images should be in one div and another (next) 5 images should be in another div..

Now I get something like this

<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">


But I need like this

<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>


How should I get this

Answer Source

Change loop like this:-

$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>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download