swaggrK
asked on
CFOUTPUT months based on current month
I am not sure if the best way to do this is in Coldfusion or SQL.
I would like to output calendar months based on the current month.
For example, with this month being April, I would like to output one month prior(March) and four months into the future(May, June, July, August).
So, my results would like March, April, May, June, July, August.
The following is the hardcoded code I am using but naturally I do not want to have to change the month name and month number every month.
<a href="page?m_id=3" onclick="change_month(3)"> <h6 class="" >March</h6></a>
<a href="page?m_id=4" onclick="change_month(4)"> <h6 class="active" >April</h6></a>
<a href="page?m_id=5" onclick="change_month(5)"> <h6 >May</h6></a>
<a href="page?m_id=6" onclick="change_month(6)"> <h6 >June</h6></a>
<a href="page?m_id=7" onclick="change_month(7)"> <h6 >July</h6></a>
<a href="page?m_id=8" onclick="change_month(8)"> <h6 >August</h6></a>
<a href="/secc_chicago/includ es/events_ by_month.c fm?m_id=9" onclick="change_month(9)"> <h6 >September</h6></a>
I would like to output calendar months based on the current month.
For example, with this month being April, I would like to output one month prior(March) and four months into the future(May, June, July, August).
So, my results would like March, April, May, June, July, August.
The following is the hardcoded code I am using but naturally I do not want to have to change the month name and month number every month.
<a href="page?m_id=3" onclick="change_month(3)">
<a href="page?m_id=4" onclick="change_month(4)">
<a href="page?m_id=5" onclick="change_month(5)">
<a href="page?m_id=6" onclick="change_month(6)">
<a href="page?m_id=7" onclick="change_month(7)">
<a href="page?m_id=8" onclick="change_month(8)">
<a href="/secc_chicago/includ
You could use either. But if it's just month name and number you need, I'd probably use ColdFusion
<!--- get date last month --->
<cfset startDate = dateAdd("m", -1, now())>
<cfoutput>
<cfloop from="0" to="4" index="x">
<cfset monthNumber = datePart("m", dateAdd("m", x, startDate))>
<!--- requires CF9. for CF8 just use cfif/cfelse ---->
<cfset className = monthNumber eq month(now()) ? "active" : "normal">
<a href="page?m_id=#monthNumb er#" onclick="change_month(#mon thNumber#) ">
<h6 class="#className#">#Month AsString(m onthNumber )#</h6>
</a>
</cfloop>
</cfoutput>
<!--- get date last month --->
<cfset startDate = dateAdd("m", -1, now())>
<cfoutput>
<cfloop from="0" to="4" index="x">
<cfset monthNumber = datePart("m", dateAdd("m", x, startDate))>
<!--- requires CF9. for CF8 just use cfif/cfelse ---->
<cfset className = monthNumber eq month(now()) ? "active" : "normal">
<a href="page?m_id=#monthNumb
<h6 class="#className#">#Month
</a>
</cfloop>
</cfoutput>
Oops.. too slow ;-) I assumed the last link was a copy / paste error. Just don't forget to adjust the css class for the "current" month.
Also typo correction, this:
<cfloop from="0" to="4" index="x">
should be:
<cfloop from="0" to="5" index="x">
Also typo correction, this:
<cfloop from="0" to="4" index="x">
should be:
<cfloop from="0" to="5" index="x">
ASKER
_agx_
The only issue seems to be that the class="active" seems to always default to April, the current month.
The only issue seems to be that the class="active" seems to always default to April, the current month.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<cfset t = DateAdd("m", -2, now()) />
<cfoutput>
<cfloop from="0" to="6" index="n">
<cfset t = DateAdd("m", 1, t) />
<a href="page?m_id=#Month(t)#
</cfloop>
</cfoutput>
....but you might need to change the final link.