Jump to content

php script to display newest 3 images?


gabriel

Recommended Posts

Hello, I am a little stuck trying to write a script that displays the newest 3 images within a 'thumbnails' folder on the home page of my website. All the thumbnail images in the folder are listed sequentially, so by newest 3 images, they could be 156.jpg, 155.jpg, and 154.jpg.

 

Then when I later upload image 157, the script would display image thumbnails 157, 156, and 155 on the home page.

 

I'm only concerned with linking the thumbnails to a single '/gallery' folder, they don't need to individually link to different places.

 

Anybody care to assist with helping to code this?

Link to comment
https://forums.phpfreaks.com/topic/133653-php-script-to-display-newest-3-images/
Share on other sites

I'd probably try looping through every folder images are in > saving the path and file name in an array > sorting the array by the file name > selecting top 3.. first idea that came to mind.

 

I don't think people will just create this little app for you, try working on an idea then coming back when you have some code and get stuck or run into problems...

 

Adam

ok well if you need it in the future pm.  from a personal point of view i think it would be easier that way then you could upload new images no matter where you are and it will alay be displaying the latest images on the page.

If the images are named between 100 and 999, you can do a simple sort on the paths returned by e.g. glob(). Actually, I believe glob() automatically sorts the paths (ascending).

 

<?php
$images = glob('/path/to/images/*.jpg');
$images = array_reverse($images);
for ($i = 0; $i < 3; $i++) {
   echo '<img src="' . $images[$i] . '" alt="" /><br />';
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.