Click to See Complete Forum and Search --> : need a simple image cycler


Matt A
03-23-2003, 10:57 AM
On a page where I have an image that I want to change daily, and an image folder with, say, a hundred .gifs (00.gif, 01.gif, 02.gif, etc)

I'm looking for a simple java function that would convert the date into the file prefix and concatinate that into the filename of the image file to load...and then load the image.

Any suggestions?

Thanks,

kindergartener Matt

khaki
03-23-2003, 12:50 PM
Hi Matt...

Something like this?

<script type="text/javascript">
var now = new(Date);
var dayImg = now.getMonth()+1 + '' + now.getDate() + ".gif";
document.write('<img src=' + dayImg + '')
</script>

Or have I over-simplified what you are asking for?
(you did ask for simple, y'know. lol)
k

Matt A
03-23-2003, 01:17 PM
hard to imagine it getting much simpler <grin> Thanks, I will give it a whirl. I'll need to check for out of range days and have a default image, I think...
I'll follow up if I run into another wall. Thanks, and Happy Spring!!

dabush
03-23-2003, 03:59 PM
good thinking khaki, but this will crreate some problmes. The files for January 11 and November 1 would both be 111.gif. This also occurs with other dates as well. Might want to try something like this.

<script type=text/javascript language=JavaScript>
<!--
var theDate = new Date;
var imgSrc = theDate.getMonth()+1+'-'+theDate.getDate()+'.gif';
document.write('<img src="'+imgSrc+'">');
// -->
</script>

Place this code where you want the image to appear. Now, files will be in the format <month>-<date>.gif

January 11 = 1-11.gif
November 1 = 11-1.gif

This way, there will be no confusion with the dates

khaki
03-23-2003, 04:19 PM
Hi dabush...

I agree... but Matt asked for simple.

This could also have been done using an if statement to add leading zeros to single digit days and/or months, arrays to alter the month number to a month name, and all sorts of other means.

The 111 issue arises once... with a 10 month span in between (121 is another example of that - even further out). If Matt is archiving that far, it would benfit him to use underscores (why do peolpe use dashes? I hate dashes in file names). Ultimately it comes down to the naming conventions that one uses when setting-out.

Personally I use leading zeros AND underscores for my own use (plus year), but we have huge archives of images (yy_mm_dd.gif).

Your point is valid though.

Matt, please let me know if you need something more complex than I already provided.

love_the_underscore... hate-the-dash...
k

dabush
03-23-2003, 04:31 PM
1-11 and 11-1 are not the only conflicts. there are more.

111
112
113
114
115
116
117
118
119
121
122
123
124
125
126
127
128
129

Matt A
03-23-2003, 05:54 PM
well...there are two possible choices..I _could_ name the files with a day_month_year scheme like 230303 for March 23 2003, Or I could give the files a three digit number and just use a randomizer to pick em....hmmm. I think the key will be to find the right naming scheme..

khaki
03-23-2003, 06:10 PM
Hi Matt...
(and dabush ;) )...

you can use this if you want (it adds the leading zeros - and the year... if you want it):

<script type="text/javascript">
var now = new(Date);
var y = now.getFullYear();
var m = now.getMonth()+1;
var d = now.getDate();
m = (m < 9) ? "0" + m : m;
d = (d < 9) ? "0" + d : d;
document.write('<img src=' + y + "_" + m + "_" + d + '.gif>');
</script>

It returns:
<img src="2003_03_23.gif">

I'm not a javascript-girl (Jersey-girl! wink) so someone better check me on this (just to be sure. lol).

(I'm sure that dabush is already checking it. wink)
k

dabush
03-23-2003, 06:13 PM
Originally posted by khaki

(I'm sure that dabush is already checking it. wink)
k

lol. yep. that is, in my opinion a very good method of doing it.

Matt A
03-23-2003, 11:07 PM
rah! from a guy who grew up near Freehold!
Thank you!!

khaki
03-24-2003, 12:21 AM
...

Nedals
03-24-2003, 12:43 AM
OK! Time to chime in.
Matt, how many images do you intend to create? Surely not one for each different day of the year, or even many years when you start talking about 2003 in your naming scheme. Secondly, do you care what image appears on what day as long as it changes?

Depending on your answers, the simple solution could be to randomly pick from 100 images. Then your naming problem is easy. (img1..img100) and randomly selecting is then easy. Just use the random number to get the image.

<script type="text/javascript">
var randomNo = Math.floor(Math.random()*100);
document.write('<img src='img' + randomNo);
</script>

Recognize it, khaki. :)

khaki
03-24-2003, 01:11 AM
Recognize it, khaki.Absolutley Nedals!!!!
Do you think that I don't pay attention? (even with Bruce groaning inside of my head)???

Hey... I don't know what Tim's needs are (I was willing to accept the "simple" need. I escalated it because of dabush... which is cool. And I enjoyed the challenged of it!!!)

I don't think that a random image is what Tim was looking to provide however...
so... he may need to name his images accordingly to satisfy some standard naming convention (even if the most recent images don't change. I do that via Excel programming... so I'd be willing to offer my solution for that if he is open to that type of solution).

I knew that this was not a cookie-cutter question. However... until Tim advises otherwise, I will keep the solutions as basic as possible.

Dabush was wise to point-out some issues... but Tim really needs to let us know what he is looking to do before we can truly help him.

Glad your on-board netals (wing-man). There's never enough expertise to bounce around for determining the proper solution.

I'm more than willing to help (as long as my help is relevent).
What's next?

willing ... (subjectively able)... and definitely interested in the solution...
k

Matt A
03-24-2003, 07:10 AM
hey eddie, can you lend me a few bucks
tonight can you get us a ride?

Here's the problem with the random scheme -- there's really no way to prevent an image from being shown twice....that I know of. I don't have access to cgi on this server....

I don't think it'll be too hard to do 2 or 3 months at a time....

and I'm just gonna throw that money on the bed
she'll see this time I wasn't just talkin

then I'm gonna go out walkin

Matt A
03-24-2003, 07:59 AM
oh yeah...is there a simple way to assign a default value if there is no image corresponding to the one the script evaluates to?