I have some code that creates variables for each of the next 12 months. This
is an example for the second month.
<CFSET VARIABLES.Date1 = DateFormat(DateAdd("M", 1, Now()))>
<CFSET VARIABLES.numDays1 = DaysInMonth(Date1)>
<CFSET VARIABLES.Month1 = Month(Date1)>
<CFSET VARIABLES.MonthString1 = MonthAsString(Month1)>
<CFSET VARIABLES.MonthStrg1 = Left(MonthString1, 3)>
<CFSET VARIABLES.Year1 = Year(Date1)>
<CFSET VARIABLES.BegDate1 = "#Month1#/1/#Year1#">
<CFSET VARIABLES.BegDate1 = CreateODBCDate(BegDate1)>
<CFSET VARIABLES.EndDate1 = "#Month1#/#numDays1#/#Year1#">
<CFSET VARIABLES.EndDate1 = CreateODBCDate(EndDate1)>
Now I could write this all out and replace the integer "1" with "2", "3" etc.
over and over but it makes sense to save typing and put this in a loop.
Problem is I can't seem to figure out how.
<CFLOOP FROM="1" TO="11" INDEX="i">
<CFSET VARIABLES.Date#i# = DateFormat(DateAdd("M", #i#, Now()))> etc. ???
</CFLOOP>
This line looks particulary tricky:
<CFSET VARIABLES.EndDate1 = "#Month1#/#numDays1#/#Year1#">
How do you put a variable in a variable? Something to do with evaluate?
surprised
Thank you,
John
Ian Skinner - 28 May 2008 16:40 GMT
This is screaming for a structure or array or maybe an array of
structures possible even an component|object.
But if you insist on doing it this way, array notation is what you are
looking for.
<cfset variables['date' & i] = dateFormat(dateAdd('M',i,now()))>
Sam (adobe certified FL and CF developer) - 28 May 2008 16:55 GMT
Use SetVariable and Evaluate function like this:
<cfset SetVariable("VARIABLES.EndDate#i#", "#Evaluate('Month' & i)#/
#Evaluate('numDays & i)#/#Evaluate('Year & i)#") />
OR just use Evaluate:
<cfset "VARIABLES.EndDate#i#" = "#Evaluate('Month' & i)#/
#Evaluate('numDays & i)#/#Evaluate('Year & i)#" />
-Sam
> I have some code that creates variables for each of the next 12 months. This
> is an example for the second month.
[quoted text clipped - 29 lines]
>
> John
Dan Bracuk - 28 May 2008 17:36 GMT
In addition to what Ian said, Cold Fusion is probably letting you get away with
a bad programming practice. DateFormat returns a string, not a date. Plus,
the fact that you are creating odbcdates inside a loop suggestst that you are
doing something inefficient elsewhere in your app.
Eivind - 29 May 2008 07:08 GMT
John Nemo skreiv:
> I have some code that creates variables for each of the next 12 months. This
> is an example for the second month.
[quoted text clipped - 9 lines]
> <CFSET VARIABLES.EndDate1 = "#Month1#/#numDays1#/#Year1#">
> <CFSET VARIABLES.EndDate1 = CreateODBCDate(EndDate1)>
Coldfusion has Arrays, storing 12 identical pieces of information for
the 12 months of the year sound like a perfect application for an array.
Using dates[monthnumber].Month to indicate the monthfield of monthnumber
rather than variables.month1 to month12 makes it much easier to loop,
since you can then do the equivalent of:
<cfloop from="1" to="12" index="i">
<cfset dates[i] = StructNew()>
<cfset dates[i].date = DateAdd("M",i,now())>
...
</cfloop>
Other than that, this does seem like a bit of a kludge -- you don't need
to massage the dates NEARLY that much for most sensible uses, for
example CreateODBCDate is in most cases completely superfluous since
<cfqueryparam> will accept a datetime object as value.
(i.e. <cfqueryparam cfsqltype="cf_sql_date" value="#now()#"> is
perfectly fine.)
Eivind
Grizzly9279 - 29 May 2008 16:50 GMT
Alternatively:
<CFSET "VARIABLES.EndDate#i#" = "#Month1#/#numDays1#/#Year1#">
Bluetone - 29 May 2008 18:27 GMT
:grin;
Thanks for all the replies folks!