Need help with random image code

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

User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

outdoorxtreme1 wrote: then I get an echo but the pictures appear randomly each time the page is refreshed.
Uhhh, isn't that what you want it to do? / Told us you wanted it to do?

Your posts title its "Need help with random image code"

So yea...
outdoorxtreme1
Forum Newbie
Posts: 18
Joined: Tue Mar 21, 2006 10:59 am

Post by outdoorxtreme1 »

In my very first post I asked if someone please tell me what I need to change or add to allow this script to display a new image weekly instead of every time the page is refreshed.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

outdoorxtreme1 wrote:In my very first post I asked if someone please tell me what I need to change or add to allow this script to display a new image weekly instead of every time the page is refreshed.
What??? Then why on earth have you been using ORDER BY RAND()?
You're going to need to set up your script to store in the database what the current image to display is, and update it weekly.

Code: Select all

if(date('N') == 7) // If today is sunday
{
    mysql_query("INSERT INTO `imageTable` SET `weekOf` = '" . date('Y-m-d') . "', `image` = (SELECT `image` FROM `links` ORDER BY RAND() LIMIT 1);");
}

// Display
$result = mysql_query("SELECT `image` FROM `imageTable` ORDER BY `weekOf` DESC LIMIT 1;");
$result = mysql_fetch_object($result);

echo '<img src="' . $result->image . '" />';
That code assumes that your page is visited at least daily, however. You'll need to come up with the logic to have it look at previous entries and their dates in order to determine whether or not it should be updated.
Post Reply