Random image per week

images php coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 6
Joined: Thu Feb 17, 2011 8:00 am

Wed Mar 09, 2011 8:12 am


I'm using the following code to display a random image per week but can't get it to display the image. It was working but I changed something and now it does not display the images. I also would like to add a link to the image. What am I doing wrong?

//first of all, get the week:
$d = date("z,w"); // day of year (0..364), day of week (0 Sunday .. 6 saturday)
list($dy,$dw) = explode(",", $d);
$week = intval(($dy-$dw)/7);
if(($dy-$dw)%7) ++$week;
// this calendar starts weeks on sundays, the first partial week is counted
// next, read the file
$images = file('imagelist.txt'); // one image per line
$thisimage = preg_replace("{\r?\n}", "", $images[$week]);
echo "<img src=$thisimage>";

$images1 = file('imagelist1.txt'); // one image per line
$thisimage1 = preg_replace("{\r?\n}", "", $images1[$week]);
echo "<img src=$thisimage1>";

php-forum Super User
php-forum Super User
Posts: 1803
Joined: Thu Mar 10, 2011 5:07 pm

Sun Mar 13, 2011 9:10 pm

Make sure that there is an image for every week in imagelist.txt and imagelist1.txt. For e.g. if $week = 10 then there should be a value for $images[10].

Post Reply