vendredi 4 janvier 2019

PHP - Displaying directory folders in random order

The code works perfectly. Just don't know how to get it to display the content randomly without using Jquery.

Got it to display a random image from the current directory, but not sure how to get the directory to display randomly.

$dir = 'img/series_thumbnail/';
$dh = opendir($dir);
while (($fileName = readdir($dh)) !== false) {
  if (!in_array($fileName, array('.', '..'))) {
    $images = glob($dir . $fileName . '/*.{jpg,jpeg,png,gif}', GLOB_BRACE);
    $imgMax = count($images)-1;
    $imgNum = rand(0, $imgMax);
    $imgUrl = $images[$imgNum];
    echo '<li>';
    echo '<img src="' . $imgUrl . '"/>';
    echo '<div class="info"><p>' . $fileName . '</p></div>';
    echo '</li>';
  }
}

Thank you.




Aucun commentaire:

Enregistrer un commentaire