1

i need to show 2 different random images. i tried this code but the second image, show the same image of the first. i have to replicate this but with 15 random images. http://www.kometschuh.de/Example-Random-CSS3-Image-Flip-Effect.html for each <li> i need to show 2 different random images

<ul class="flip">
  <?php
    $all_images = glob("wp-content/themes/connexia/img-test/{*.jpg, *.JPG, *.JPEG, *.png, *.PNG}", GLOB_BRACE);

    shuffle($all_images);

    foreach ($all_images as $index => $image ) {
     if ($index == 15) break;  // Only print 15 images
     $image_name = basename($image);
     $image_name2 = basename($image++);
     echo "<li>
               <img src='/wp-content/themes/connexia/img-test/{$image_name}' />
               <img src='/wp-content/themes/connexia/img-test/{$image_name2}' />
          </li>";
    }
  ?>
</ul>
manu
  • 93
  • 2
  • 11

1 Answers1

5

Try this:

$imagesDir = 'images/tips/';

$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);

$randomImage = $images[array_rand($images)]; // See comments

You can send a 2nd argument to array_rand() to get more than 1.

Also have a look at these links:

http://askwebexpert.com/tutorials/how-to-display-random-images-from-a-directory-using-php/

php generate random image from a directory

PHP pull random image from folder

Community
  • 1
  • 1
Dave
  • 967
  • 10
  • 23
  • i have to replicate this but with 15 random images. http://www.kometschuh.de/Example-Random-CSS3-Image-Flip-Effect.html for each
  • i need to show 2 different random images
  • – manu Nov 24 '15 at 14:31
  • You can still use this, request 2 random images for each
  • iteration
  • – Dave Nov 24 '15 at 15:01