0

I have a folder containing images and echo them to the page.
This works fine however I want them to randomly change output upon page load.
This is the part that outputs the images:

<?php 
$directory = 'somewhere/images/';
$files = getPathsByKind($directory,'svg');
if(!empty($files)) {
    for($i=0; $i < 32; $i++){
        echo '<img src="'.get_bloginfo('template_directory').'/images/'.$files[$i].'">';
    }
}
?>

So I thought I'd had to use rand() or array_rand() but both throw me errors. Which is fine but I have no idea on how to fix them. rand() throws me an second parameter error. (max andmin value). Since this is an image there is no second parameter. array_rand() won't work because of the loop it isn't an Array anymore.

Any idea on how to accomplish this?

Interactive
  • 1,474
  • 5
  • 25
  • 57

4 Answers4

2

Can't you use shuffle function to randomize the array and then output them with your code?

Something like that:

<?php 
$directory = 'somewhere/images/';
$files = getPathsByKind($directory,'svg');
if(!empty($files)) {
    shuffle($files);
    for($i=0; $i < 32; $i++){
        echo '<img src="'.get_bloginfo('template_directory').'/images/'.$files[$i].'">';
    }
}
?>

http://php.net/manual/de/function.shuffle.php

dns_nx
  • 3,651
  • 4
  • 37
  • 66
2

You question is not clear if you want only 1 image or want all images in a random order. ( I'm going with all images in a random order );

$directory = 'somewhere/images/';
$files = getPathsByKind($directory,'svg');

if(!empty($files)) {

    shuffle($files); //randomly sort array

    for($i=0; $i < 32; $i++){
        echo '<img src="'.get_bloginfo('template_directory').'/images/'.$files[$i].'">';
    }
}

You can see an example of shuffle here

https://www.w3schools.com/php/func_array_shuffle.asp

PS

So I thought I'd had to use rand() or array_rand() but both throw me errors. Which is fine but I have no idea on how to fix them

Without seeing what you had I can only guess, my bet would be you tried doing array_rand() on something other then $files ( possibly inside the loop).

and rand() just generates psudorandom number between $min and $max so if you did rand(1,10) you would expect a number from 1-10.

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
1

Just Use This

<?php 
    $directory = 'somewhere/images/';
    $files = getPathsByKind($directory,'svg');
    if(!empty($files)) {
        shuffle($files); //This will sort your array....
        for($i=0; $i < 32; $i++){
            echo '<img src="'.get_bloginfo('template_directory').'/images/'.$files[$i].'">';
        }
    }
?>
GYaN
  • 2,327
  • 4
  • 19
  • 39
1

well I'm kind of looking into the possibility to random output the $i value

You can create an array with number 0->32 and shuffle it, the use foreach to echo the pictures.

Insert this inside your if(!empty()) statement:

$arr = range(0,32);
shuffle($arr);

foreach($arr as $i){
    echo '<img src="'.get_bloginfo('template_directory').'/images/'.$files[$i].'">';
}
Andreas
  • 23,610
  • 6
  • 30
  • 62