Sponsored By

Sign in to follow this  
bobgo2728

Programming Help

Recommended Posts

OK I found this script that will do a image of the day. It changes over at midnight of the server time. Which is fine with me. Now I am wondering how I can have it 1) display the images in a calander view below so that way there are thumbnails. 2) how to have it be able to load up more then 31 images so I don't have to change it for several months down the road. Anyway here is the code.

<?php

$totalPics="31";

$photoOfTheDay = Array (

'1' => 'files/photo1.jpg' ,
'2' => 'files/photo2.jpg' ,
'3' => 'files/photo3.jpg' ,
'4' => 'files/photo4.jpg' ,
'5' => 'files/photo5.jpg' ,
'6' => 'files/photo6.jpg' ,
'7' => 'files/photo7.jpg' ,
'8' => 'files/photo8.jpg' ,
'9' => 'files/photo9.jpg' ,
'10' => 'files/photo10.jpg' ,
'11' => 'files/photo11.jpg' ,
'12' => 'files/photo12.jpg' ,
'13' => 'files/photo13.jpg' ,
'14' => 'files/photo14.jpg' ,
'15' => 'files/photo15.jpg' ,
'16' => 'files/photo16.jpg' ,
'17' => 'files/photo17.jpg' ,
'18' => 'files/photo18.jpg' ,
'19' => 'files/photo19.jpg' ,
'20' => 'files/photo20.jpg' ,
'21' => 'files/photo21.jpg' ,
'22' => 'files/photo22.jpg' ,
'23' => 'files/photo23.jpg' ,
'24' => 'files/photo24.jpg' ,
'25' => 'files/photo25.jpg' ,
'26' => 'files/photo26.jpg' ,
'27' => 'files/photo27.jpg' ,
'28' => 'files/photo28.jpg' ,
'29' => 'files/photo29.jpg' ,
'30' => 'files/photo30.jpg' ,
'31' => 'files/photo31.jpg' ,

);

// Place day of month in variable 'x'
//$x=date("d");
// A website vistor made a suggestion to change the date function to use j
// instead of d. This causes the date to be read without leading zeros.
$x=date("j");

// If value of date exceeds the amount of photos then pick a random photo to display
if($x > $totalPics) { $x=rand(1,$totalPics); }

// Display image
echo <<<EOC
<center><img src="$photoOfTheDay[$x]"></center>
EOC;

?>

Share this post


Link to post
Share on other sites

1) This is the part of the code that displays the image:

// Display image
echo <<<EOC
<center><img src="$photoOfTheDay[$x]"></center>
EOC;

This is really the only necessary part:

<img src="$photoOfTheDay[$x]">

2) Change the number listed here:

$totalPics="31";

Then just add a new entry to the bottom of the array with the next number in the sequence.

'31' => 'files/photo31.jpg' ,
'32' => 'files/photo32.jpg' ,

Edited by jsbowen

Share this post


Link to post
Share on other sites

The array is unnecessary.

$totalPics = 30;

function photoOfTheDay($n)
{
global $totalPics;
$n = $n > $totalPics ? rand(1, $totalPics) : $n;
return 'files/photo' . strval($n) . '.jpg';
}

You could span several months by having separate directories for each month, passing the month number into the function, and returning "files/photo/$month/$day.jpg". Or one directory with "photo$month$day.jpg". Or by using the day of the year (date('z')). Or by using "$month * 31 + $day;". Or....

Edited by jcl

Share this post


Link to post
Share on other sites

OK give me an example of what the code would look like. I would like to have this thing be abled to be loaded up for several months down the road.

The array is unnecessary.

$totalPics = 30;

function photoOfTheDay($n)
{
global $totalPics;
$n = $n > $totalPics ? rand(1, $totalPics) : $n;
return 'files/photo' . strval($n) . '.jpg';
}

You could span several months by having separate directories for each month, passing the month number into the function, and returning "files/photo/$month/$day.jpg". Or one directory with "photo$month$day.jpg". Or by using the day of the year (date('z')). Or by using "$month * 31 + $day;". Or....

Share this post


Link to post
Share on other sites

The code for the day-of-the-year version would identical to the code post except that the range of the $n parameter would be [0,365] instead of [1,31].

The code for the day-of-the-month would be something like

function photoOfTheDay($month, $day)
{
return 'files/photo/' . strval($month) . '/' . strval($day) . '.jpg';
}

photoOfTheDay(3, 15) would yield "files/photo/3/15.jpg". The range checks would be complicated...

...however, the range checks above don't work, anyway: you get broken links on valid dates if the files don't exist. The script could check for the files, but it might be easier to omit the check and look for broken links in the generated pages.

Edited by jcl

Share this post


Link to post
Share on other sites

So do I use this code that you have given me or does it not work?

The code for the day-of-the-year version would identical to the code post except that the range of the $n parameter would be [0,365] instead of [1,31].

The code for the day-of-the-month would be something like

function photoOfTheDay($month, $day)
{
return 'files/photo/' . strval($month) . '/' . strval($day) . '.jpg';
}

photoOfTheDay(3, 15) would yield "files/photo/3/15.jpg". The range checks would be complicated...

...however, the range checks above don't work, anyway: you get broken links on valid dates if the files don't exist. The script could check for the files, but it might be easier to omit the check and look for broken links in the generated pages.

Share this post


Link to post
Share on other sites
So do I use this code that you have given me or does it not work?

It seems to work.

Share this post


Link to post
Share on other sites

I am just not following your code. I mean if you can edit what I gave the 1st time and show me the complete code so I can see it exactly what it needs to look like.

So do I use this code that you have given me or does it not work?

It seems to work.

Share this post


Link to post
Share on other sites

Month and day version:

<?php

function photoOfTheDay($month, $day)
{
return 'files/photo/' . strval($month) . '/' . strval($day) . '.jpg';
}

$date = getdate();
$photo = photoOfTheDay($date[month], $date[mday]);

?>

<center>
<img src="<?php echo $photo ?>">
</center>

Day of the year version:

<?php

function photoOfTheDay($day)
{
return 'files/photo/' . strval($day) . '.jpg';
}

$date = getdate();
$photo = photoOfTheDay($date[yday]);

?>

<center>
<img src="<?php echo $photo ?>">
</center>

Share this post


Link to post
Share on other sites

OK got it, now what do I call the images and what folders would I or need to put them into.

Month and day version:

<?php

function photoOfTheDay($month, $day)
{
return 'files/photo/' . strval($month) . '/' . strval($day) . '.jpg';
}

$date = getdate();
$photo = photoOfTheDay($date[month], $date[mday]);

?>

<center>
<img src="<?php echo $photo ?>">
</center>

Day of the year version:

<?php

function photoOfTheDay($day)
{
return 'files/photo/' . strval($day) . '.jpg';
}

$date = getdate();
$photo = photoOfTheDay($date[yday]);

?>

<center>
<img src="<?php echo $photo ?>">
</center>

Share this post


Link to post
Share on other sites

The script can be changed to whatever you need. The month and day version above uses "files/photo/<month>/<day>.jpg" where <month> and <day> are integers in the ranges [1,12] and [1,31], resp. The day of the year version uses "files/photo/<day>.jpg" where <day> is an integer in the range [0,365].

If you use either script, you might want to fix the inconsistent pluralization of "files" and "photo". I don't know why I did that.

Edited by jcl

Share this post


Link to post
Share on other sites

OK but is there an easy way also to add in a calendar view below it with thumbnails?

The script can be changed to whatever you need. The month and day version above uses "files/photo/<month>/<day>.jpg" where <month> and <day> are integers in the ranges [1,12] and [1,31], resp. The day of the year version uses "files/photo/<day>.jpg" where <day> is an integer in the range [0,365].

If you use either script, you might want to fix the inconsistent pluralization of "files" and "photo". I don't know why I did that.

Share this post


Link to post
Share on other sites
OK but is there an easy way also to add in a calendar view below it with thumbnails?

No idea.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this