To load image conditionally

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pirolilla
Forum Newbie
Posts: 13
Joined: Sat Jan 24, 2004 1:24 am

To load image conditionally

Post by pirolilla »

I need aid with another one script PHP.

In a page an image is due to open (the cover of a newspaper) that the website who lodges it changes every day. The name of the image contains reference to the date of every day, but the image is incorporated last hours of the new day, with which the first hours of every day the new image not yet exists.

I use script PHP so that it changes the name of the image in agreement with the date, but need that the new image was not loaded until this one exists in that website. Script that I need would have east scheme:

If imagendate24.gif exists load imagendate24.gif, if imagendate24.gif does not exist load imagendate23.gif

Understanding that date24 makes reference to the present day and date23 to the previous day.

In my script PHP I have loaded a variable, that is:

$datemonth = date(d);

and it would create another one, that would be:

$datemonth = date(d) -1;

Thanks for your aid.
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

Your code might look like this.

Code: Select all

<?php
$day = date(d);
while (!file_exists("someimage".$day.".jpg")) {
     // Keep looping til a picture is available
     // In the loop decrement the $day until we find a day that has a picture.
     $day--;
}

// If we're out of the loop, then there is a picture for the last $day.
$validpicture = "someimage".$day.".jpg";
?>
This has not been tested at all.
pirolilla
Forum Newbie
Posts: 13
Joined: Sat Jan 24, 2004 1:24 am

Post by pirolilla »

I will try this. Thanks.
Post Reply