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
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

Link to comment
Share on other sites

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 />';
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.