/    Sign up×
Community /Pin to ProfileBookmark

auto daily comic image changer

i have a seven day image changer on my comic site
( [url]http://fatbottoms.atspace.com[/url] ) now. is it possible to change this existing code so that i can increase it (up to 2 weeks or even a month )to however long i desire? if so, how would i do this?

here’s the code:
<script>
<!–
var mondayimg=”fb124.gif”
var tuesdayimg=”fb125.gif”
var wednesdayimg=”fb126.gif”
var thursdayimg=”fb127.gif”
var fridayimg=”fb121.gif”
var saturdayimg=”fb122.gif”
var sundayimg=”fb123.gif”
var mydate=new Date()
var today=mydate.getDay()
if (today==1)
document.write(‘<img src=”‘+mondayimg+'”>’)
else if (today==2)
document.write(‘<img src=”‘+tuesdayimg+'”>’)
else if (today==3)
document.write(‘<img src=”‘+wednesdayimg+'”>’)
else if (today==4)
document.write(‘<img src=”‘+thursdayimg+'”>’)
else if (today==5)
document.write(‘<img src=”‘+fridayimg+'”>’)
else if (today==6)
document.write(‘<img src=”‘+saturdayimg+'”>’)
else
document.write(‘<img src=”‘+sundayimg+'”>’)
//–>
</script>

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 05.2005 — Hmmm...looks like JavaScript. Did you mean to post this in the JS forum, or are you looking for a PHP alternative?
Copy linkTweet thisAlerts:
@fatbottomsauthorJun 06.2005 — yeah, i thought it was javascript, but someone suggested that php would be more relevant regarding my auto image changer. if there is an alternative in php, what would it be and would i have to rename all my files (i have over 400 files) ?
Copy linkTweet thisAlerts:
@NogDogJun 06.2005 — If what you're looking for is a different image each day, yes, it would be fairly simple with PHP. You would only have to change the html page on which this "random" image is displayed, renaming it to have a .php suffix instead of .html. Then you'd need a little PHP code where the image will go. Are all the images in the same directory? If so, you could do something like this:
[code=php]
<!-- HTML stuff up to the point where you want the image to display goes here, then... -->

<?php
# grab list of image files into array $images:
if ($handle = opendir('/img')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$images[] = $file;
}
}
closedir($handle);
# get days since UNIX epoch start:
$days = floor(time() / (60*60*24));
# select image number:
$imgNbr = $days % count($images);
# display image:
echo "<img src='{$images[$imgNbr]}' alt='Today's Comic'>n";
}
else
{
echo "Oops! Today's Comic was supposed to display here, but something went wrong.";
}
?>

<!-- rest of your HTML for the page continues here -->
[/code]
Copy linkTweet thisAlerts:
@fatbottomsauthorJun 06.2005 — it sounds like i would still have to change the suffix like you said for all my html. not looking forward to that, as i have over 400 files. just to make sure i got this right. would this be correct :

<?php

# grab list of image files into array $images:


if ($handle = opendir('/img')) {

while (false !== ($file = readdir($handle))) {

if ($file != "." && $file != "..") {

$images[] = $file;

}

}

closedir($handle);

# get days since UNIX epoch start:


$days = floor(time() / (60*60*24));

# select image number:


$imgNbr = $days % count($images);

# display image:


echo "<img src='{fb124.php}' alt='Today's Comic'>n";

}

echo "<img src='{fb125.php}' alt='Today's Comic'>n";

}

echo "<img src='{fb126.php}' alt='Today's Comic'>n";

}

<--- etc. etc.---->

else

{

echo "Oops! Today's Comic was supposed to display here, but something went wrong.";

}

?>



here is what someone wrote for the javascript code. would this be easier?

<script language="JavaScript" type="text/javascript">

<!--

// To find every 30 days in year

var today=new Date();

var date=new Date(today.getFullYear(),0,0);

var days30=Math.floor((today.getTime()-date.getTime())/1000/60/60/24/30)

function MyFunction(){

if (days30==0)

document.write('<img src="'+days30_0+'">')

if (days30==1)

document.write('<img src="'+days30_1+'">')

else if (days30==2)

document.write('<img src="'+days30_2+'">')

else if (days30==3)

document.write('<img src="'+days30_3+'">')

else if (days30==4)

document.write('<img src="'+days30_4+'">')

else if (days30==5)

document.write('<img src="'+days30_5+'">')

else if (days30==6)

document.write('<img src="'+days30_6+'">')

//........to days30==12

else

document.write('<img src="'+days30_Default+'">')

}

//-->

</script>
Copy linkTweet thisAlerts:
@NogDogJun 06.2005 — No, the idea I was proposing would pick out one image file from your image directory and display it at that point in the page. The images would still be .gif images. The only .php page would be the actual page on which the image is displayed (i.e. if it's index.html it would now be index.php).

You wouldn't have to list every possible image file in the script, it's reading the img directory and grabbing every file name in there. Then it's using the number of days since the "UNIX epoch" (the day that UNIX starts counting seconds from) to do a little math and pick one image name from the array to be displayed within the IMG tag.

The only thing you should have to do is change the '/img' value in this line...
[code=php]if ($handle = opendir('/img')) {[/code]
...to whatever the correct directory name is for you images. (If you have non-images in that directory, we'll have to add a little more code to pick out only the image files.)

The whole idea here is that you don't have to change the script as you add/delete images files to/from the img directory.
Copy linkTweet thisAlerts:
@fatbottomsauthorJun 06.2005 — ah, a sliver of light is shining through. i would only have to change my index.html to index.php, where the image change occurs, right?

and no, my images are not in the image folder, i have all my files on the main directory with no folders. should i sort my images to an images folder? if not, what is the code for all the files in the same directory? i'm really new to this, so if you could actually insert the file (say, "fb124.gif" "fb125.gif" in the example), it would really help me.

here is my index page

http://fatbottoms.atspace.com
Copy linkTweet thisAlerts:
@NogDogJun 06.2005 — This should (hopefully) pull out only the .gif and .jpg files from the current directory:
[code=php]
<?php
# grab list of image files into array $images:
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
#grab only .gif and .jpg files:
if (stripos($file, ".gif") > 0 or stripos($file, ".jpg") > 0) {
$images[] = $file;
}
}
closedir($handle);
# get days since UNIX epoch start:
$days = floor(time() / (60*60*24));
# select image number:
$imgNbr = $days % count($images);
# display image:
echo "<img src='{$images[$imgNbr]}' alt='Today's Comic'>n";
}
else
{
echo "Oops! Today's Comic was supposed to display here, but something went wrong.";
}
?>
[/code]
Copy linkTweet thisAlerts:
@fatbottomsauthorJun 07.2005 — hey, sorry for putting you through the trouble of writing the code, but i just discovered my free hosting doesn't support php, so it has to be javascript. thanks for replying , but unless you can write javascript , i can't use it.
Copy linkTweet thisAlerts:
@RiannaAug 06.2006 — Hey bottoms I like your script at the top of this page. Do you have the code to access it from the body of my page? I'd like to call it as an external javascript instead of putting the whole thing into the div of my page.

Please let me know. I am only using it to try to change an image on my page daily. Thanks, Rianna
×

Success!

Help @fatbottoms spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...