-1

I am trying to set up a slideshow but want to have it dynamic that all someone has to do is add an image to the folder to have it included in the slideshow.

This is part of what I have so far and it works as expected. However, I want to include latest images first, at present it goes by file name of what is first alphabetically.

foreach(glob('media/slides/*.JPG') as $image){
    if($image_count <'15'){
        echo '<img class="slides" src="'.$image.'">';
    }
    $image_count++;
}

I can get the date from the meta like so:

$exif_data = exif_read_data($image);
echo $exif_data['FileDateTime'];

But not sure how to go about ordering them accordingly.

halfer
  • 19,824
  • 17
  • 99
  • 186
Paddy Hallihan
  • 1,624
  • 3
  • 27
  • 76
  • Possible duplicate of https://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php – 04FS Feb 20 '19 at 10:30
  • But it is probably not a good idea to do it this way in the first place, because opening each file and reading the exif data from it is a rather costly operation, so that is perhaps not a thing you should do on every single page load to begin with. – 04FS Feb 20 '19 at 10:31

1 Answers1

1

If the method you are using to retrieve a set of data doesn't support sort by itself, the staple approach is to store data in a array and sort it.

For an arbitrary way to sort you can use usort(), which allows you to provide completely custom sort logic to compare and reorganize the items in array.

Rarst
  • 2,335
  • 16
  • 26