Page 1 of 1

Automatically select "next" image from database daily?

Posted: Wed May 07, 2008 10:20 pm
by dbarak
Hi everyone,

I'm fairly new to PHP and MySQL, and there's something I'm trying to do that I can't quite figure out. If anyone can provide some insight I'd appreciate it.

What I want to do is store an image file name in a MySQL database, linked to a sequentially-assigned number (1, 2, 3, etc.) that would be the primary key. Then, in the PHP code that displays the image, I want to retrieve and display the data associated with primary key #1. The next day (at midnight?) I want to display the next image, associated with primary key #2. Then after that, the image associated with #3, etc.

It sounds like this is what I need to have happen:

PHP code, when the page is called, displays image associated with primary key #1.
Server date/time advances to the next day.
PHP code, sensing the server date/time, increments the primary key up by one. When the page is called, it displays the image associated with primary key #2.

So, first, does this logic sound right?
Second, any direction I should follow when writing this?

I'm just now learning PHP and MySQL, so any assistance would be wonderful. Thanks!

Dave

Re: Automatically select "next" image from database daily?

Posted: Thu May 08, 2008 1:50 am
by Christopher
I think it will be easier to do a modulo of the day of the year rather than what you are trying to do. We get similar questions frequently and it is not really a database problem.

Re: Automatically select "next" image from database daily?

Posted: Thu May 08, 2008 2:34 am
by Apollo
$day = time() / (24*60*60);
$day %= $numberOfImages;
Now get your image of the day with this SQL query: "SELECT image FROM images ORDER BY id LIMIT $day,1"

The "ORDER BY id LIMIT $day,1" instead of simply "WHERE id=$day" is to avoid problems when certain id's don't exist, or if id doesn't start with 0, etc.

The $numberOfImages can be queried with "SELECT COUNT(*) FROM images"

Re: Automatically select "next" image from database daily?

Posted: Thu May 08, 2008 9:31 am
by dbarak
Thanks arborint and Apollo! You've both given me some good directions to follow. At first, I couldn't remember what modulo meant, then I remembered reading about it a few months ago and realizing that it could do some pretty cool things. :D