php - Foreach loop: split elements in groups of five and six -
i'm using foreach loop output images fetched instagram api. want split outputted images in groups of four , five , in addition add div element each group except last 1 2 div's should added, following structure:
<div class="group> <div class="extra"> </div> <img/> <img/> <img/> <img/> </div> <!-- close group of 5 --> <div class="group> <div class="extra"> </div> <img/> <img/> <img/> <img/> <img/> </div> <!-- close group of 6 --> <div class="group> <div class="extra"> </div> <img/> <img/> <img/> <div class="extra"> </div> </div> <!-- close group of 5 -->
by using modulus in loop i've manage group elements correct number of items in each group, but when try add div each group breaks counting , i'm no longer getting correct number of elements grouped together. code i'm using print out images (12 in total):
<div class="group"> <!-- open group-wrapper --> $count = 0; foreach($decoded_results['data'] $item) { if ($count % 11 == 0 || $count % 11 == 5 ) { echo '<div class="extra"> </div>'; echo '</div> <div class="group">'; } echo '<a target="_blank" href=' .$link_url. '>'; echo '<div class="img-wrapper">'; echo '<img src="'.$image_link.'" /> </div> </a>'; $count++; }
any suggestion how can achieved?
this enough point in correct direction:
<div class='group'> <div class='extra'></div> <?php $i = 1; $j = 1; $step = 4; $count = count($decoded_results['data']); foreach($decoded_results['data'] $k => $v) { ?> <a target='_blank' href='<?php echo $v;?>'> <div class='img-wrapper'> <img src='<?php echo $v;?>' /> </div> </a> <?php if (($j % $step) == 0 && $i < $count) { ?> </div> <div class='group'> <div class="extra"></div> <?php $step = ($step == 4) ? 5: 4; $j = 0; } $j++; $i++; } ?> <div class='extra'></div> </div>
on side note should aware having div
(block element) inside a
(inline element) isn't semantic html, still accepted nowadays. tend avoid , can using css.
Comments
Post a Comment