breaking output of php array into chunks -
breaking output of php array into chunks -
i have array containing info images, using print number of img html tags foreach loop follows:
<?php foreach ( $images $image ) : ?> <img alt="<?php echo $image->alttext ?>" src="<?php echo $image->thumbnailurl ?>" /> <?php endforeach; ?>
i wrap div around every 10 images. remainder should div well.
i thinking need utilize array_chunk
on $images
, wrap above in loop each chunk. little bit of math did start follows:
$pics_per_page = 10; $imgcount = count($images); $num_pages = ceil($imgcount/$pics_per_page); $pages = array_chunk($images, $num_pages);
how proceed here? how utilize $images array correctly, if @ all, in outputting html?
forgetting array_chunk method, have next way, array_chunk method seems cleaner.
for 1 num_pages: echo "<div>" j = (i-1) * pics_per_page; while j <= * pics_per_page echo "<img src = "$images[j]->thumbnailurl />"; endwhile; echo "</div>"
thanks help in advance, sepehr
you using array_chunk
[docs] wrongly. have specify size of each chunk:
$chunks = array_chunk($images, 10);
then need nested for
(foreach
) loop:
<?php foreach($chunks $chunk): ?> <div class="someclass"> <?php foreach($chunk $image): ?> <img alt="<?php echo $image->alttext ?>" src="<?php echo $image->thumbnailurl ?>" /> <?php endforeach; ?> </div> <?php endforeach; ?>
php arrays
Comments
Post a Comment