this loop displays 10 images in rotation on a page.
<?php
$myImagesList = array (
'image1.png' ,
'image2.png' ,
'image3.png' ,
'image4.png' ,
'image5.png' ,
'image6.png' ,
'image7.png' ,
'image8.png' ,
'image9.png' ,
'image10.png'
);
shuffle ($myImagesList);
for ($i=0; $i<15; $i++) {
echo '' . $myImagesList[$i] . '';
}
?>
But how to divide it into two parts, so I show 5 pictures at the top of the page, and 5 images in the page footer? something like this:
<Body>
<Div id = "site">
<Div id = "header">
<? php
XXXX XXXX Show 5 images
?>
</ Div>
<Div id = "content">
Text
</ Div>
<Div id = "footer">
<? php
XXXX XXXX Show 5 images
?>
</ Div>
</ Div>
You can simply change your iteration starting point:
<?php
$myImageList = [
'image1.png',
'image2.png',
'image3.png',
'image4.png',
'image5.png',
'image6.png',
'image7.png',
'image8.png',
'image9.png',
'image10.png',
];
shuffle($myImageList);
Then in your HTML:
<div id="header">
<?php for ($i = 0; $i < 5; $i++) echo $myImagesList[$i] . '<br>'; ?>
</div>
<div id="footer">
<?php for ($i = 5; $i < 10; $i++) echo $myImagesList[$i] . '<br>'; ?>
</div>
If you are looking to show precisely half on top and half on bottom regardless of how many pieces are in $myImagesList
then you can do this:
<?php
$myImagesList = array (
'image1.png' ,
'image2.png' ,
'image3.png' ,
'image4.png' ,
'image5.png' ,
'image6.png' ,
'image7.png' ,
'image8.png' ,
'image9.png' ,
'image10.png'
);
shuffle ($myImagesList);
$firstHalf = array_slice($myImagesList, 0, round((count($myImagesList) / 2), 0, PHP_ROUND_HALF_UP));
$secondHalf = array_slice($myImagesList, round((count($myImagesList) / 2), 0, PHP_ROUND_HALF_UP));
// First half
foreach($firstHalf as $v)
{
echo $v;
}
// Second half
foreach($secondHalf as $v)
{
echo $v;
}
If an odd number of elements exists in $myImagesList
then the first half will show one image more than the bottom half.
Array slice with helper function.
<?php
$myImageList = [
'image1.png',
'image2.png',
'image3.png',
'image4.png',
'image5.png',
'image6.png',
'image7.png',
'image8.png',
'image9.png',
'image10.png',
];
function display_images(array $images, $base_path) {
?>
<?php foreach($images as $image) { ?>
<img src="<?php echo $base_path . '/' . $image ?>">
<?php } ?>
<?php
}
shuffle($myImageList);
display_images(array_slice($myImageList, 0, 5), '/foo');
display_images(array_slice($myImageList, 5, 5), '/foo');