Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to display photos of the day?

0 views
Skip to first unread message

jeffry s

unread,
Jan 29, 2008, 5:33:25 AM1/29/08
to php-g...@lists.php.net
sorry if this question sound stupid.
i need a good, simple and efficient function to display lets say photo of
the day.

i have a mysql table contain data about 1000 rows. i want to display any of
the photos randomly
and it is fixed for one day.

anyone know how to write the function that return a fixed table id for the
day?

Steve Edberg

unread,
Jan 29, 2008, 5:56:57 AM1/29/08
to jeffry s, php-g...@lists.php.net


What I would do is something like this (assuming your table has a
column 'filename' in it):

Create a cron job (on windows, I think the command is called 'at'?)
that runs this query

select filename from photo_table order by rand() limit 1

once per day, then copies that file to a predefined location (eg
images/pic_of_the_day.jpg).

Then, your web page simply refers to images/pic_of_the_day.jpg. The
contents of pic_of_the_day.jpg change every time the cronjob runs
(unless you randomly pick the same picture twice; not likely with
1000 rows, but you could include some sort of flag [eg; last used
date] to avoid picking the same image twice, or to cycle through all
images before reusing them).

This requires one database hit per day, returning one row, so the
load is next to nothing.

The cronjob could be written in any language, but since this is a PHP
list you'll have to promise to write it in PHP ;)

steve


--
+--------------- my people are the people of the dessert, ---------------+
| Steve Edberg http://pgfsun.ucdavis.edu/ |
| UC Davis Genome Center sbed...@ucdavis.edu |
| Bioinformatics programming/database/sysadmin (530)754-9127 |
+---------------- said t e lawrence, picking up his fork ----------------+

Zoltán Németh

unread,
Jan 29, 2008, 5:53:16 AM1/29/08
to jeffry s, php-g...@lists.php.net

Paul Scott

unread,
Jan 29, 2008, 7:09:58 AM1/29/08
to jeffry s, php-g...@lists.php.net

On Tue, 2008-01-29 at 18:33 +0800, jeffry s wrote:

> i have a mysql table contain data about 1000 rows. i want to display any of
> the photos randomly
> and it is fixed for one day.
>

MySQL has a rand() function, so you could bomb that off as a select once
a day on cron or something, or you could do a regular select and
array_rand() it in PHP.

--Paul

tedd

unread,
Jan 29, 2008, 11:41:19 AM1/29/08
to php-g...@lists.php.net
At 6:33 PM +0800 1/29/08, jeffry s wrote:


jeffry:

Simple enough:.

1. Figure out what day it is.

2. Pull a random image from the database if that date has changed.

If it were me, I would create a simple field in the database that
would have todays date (day of the year) in it.

Then my script would check date(z) with that field. If the value is
different, then I would replace that value with the new value and
then change the picture accordingly by using the rand() function.

Cheers,

tedd

PS: date(z) produces the day of the year (1-365)
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com

Per Jessen

unread,
Jan 29, 2008, 11:48:10 AM1/29/08
to php-g...@lists.php.net
jeffry s wrote:

> sorry if this question sound stupid.
> i need a good, simple and efficient function to display lets say photo
> of the day.
>
> i have a mysql table contain data about 1000 rows. i want to display
> any of the photos randomly and it is fixed for one day.

I use apache for that sort of thing:

.htaccess:
RewriteEngine on
RewriteCond todaysphoto.jpeg !-s
RewriteRule picktodaysphoto.php

"picktodaysphoto.php" selects the photo of the day, writes it
as 'todaysphoto.jpeg', and then redirects to it.

'todaysphoto.jpeg' is then deleted once a day by cron.


/Per Jessen, Zürich

0 new messages