gabriel Posted November 21, 2008 Share Posted November 21, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/133653-php-script-to-display-newest-3-images/ Share on other sites More sharing options...
Adam Posted November 21, 2008 Share Posted November 21, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/133653-php-script-to-display-newest-3-images/#findComment-695365 Share on other sites More sharing options...
gabriel Posted November 21, 2008 Author Share Posted November 21, 2008 All the images are in a single folder. So the script would likely just have to see what the three largest numbered images are and then display them on a page. I'm new to php so this is pretty confusing to me. Any help would be appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/133653-php-script-to-display-newest-3-images/#findComment-695370 Share on other sites More sharing options...
kev wood Posted November 21, 2008 Share Posted November 21, 2008 why not stor the paths to the images in a mysql db and then run a query on the db to only display the last 3 add images within the db? Quote Link to comment https://forums.phpfreaks.com/topic/133653-php-script-to-display-newest-3-images/#findComment-695376 Share on other sites More sharing options...
gabriel Posted November 21, 2008 Author Share Posted November 21, 2008 I upload multiple images a day via FTP. Adding a db into the mix would probably complicate things... Quote Link to comment https://forums.phpfreaks.com/topic/133653-php-script-to-display-newest-3-images/#findComment-695379 Share on other sites More sharing options...
kev wood Posted November 21, 2008 Share Posted November 21, 2008 i can give you a script that uploads the image to a specified location and stores the path to the image in the db. would that save some of your complications? Quote Link to comment https://forums.phpfreaks.com/topic/133653-php-script-to-display-newest-3-images/#findComment-695386 Share on other sites More sharing options...
gabriel Posted November 21, 2008 Author Share Posted November 21, 2008 I appreciate the offer, but I'd rather not use a db at this time. Quote Link to comment https://forums.phpfreaks.com/topic/133653-php-script-to-display-newest-3-images/#findComment-695388 Share on other sites More sharing options...
kev wood Posted November 21, 2008 Share Posted November 21, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/133653-php-script-to-display-newest-3-images/#findComment-695394 Share on other sites More sharing options...
thebadbad Posted November 21, 2008 Share Posted November 21, 2008 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 />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/133653-php-script-to-display-newest-3-images/#findComment-695396 Share on other sites More sharing options...
gabriel Posted November 21, 2008 Author Share Posted November 21, 2008 This works perfectly, thanks SO MUCH! Not sure what you mean about the names 100 - 999, as it works with images titled differently. Thanks again so much for the help with this small script! Quote Link to comment https://forums.phpfreaks.com/topic/133653-php-script-to-display-newest-3-images/#findComment-695407 Share on other sites More sharing options...
thebadbad Posted November 21, 2008 Share Posted November 21, 2008 Cool, it's just because Windows tends to sort numbers the wrong way when they are of unequal length. But it sounds believable that glob() is doing a better job Quote Link to comment https://forums.phpfreaks.com/topic/133653-php-script-to-display-newest-3-images/#findComment-695413 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.