user5599832
user5599832

Reputation:

How to show two different random images in PHP?

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>

Upvotes: 1

Views: 278

Answers (1)

Dave
Dave

Reputation: 977

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

Upvotes: 4

Related Questions