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
Automatically select "next" image from database daily?
Moderator: General Moderators
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Automatically select "next" image from database daily?
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.
(#10850)
Re: Automatically select "next" image from database daily?
$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"
$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?
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. 