Solved

CFOUTPUT months based on current month

Posted on 2012-04-12
5
329 Views
Last Modified: 2012-04-13
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/includes/events_by_month.cfm?m_id=9" onclick="change_month(9)"><h6 >September</h6></a>
0
Comment
Question by:swaggrK
  • 3
5 Comments
 
LVL 4

Expert Comment

by:JohnHowlett
ID: 37838425
How about something like:

<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)#" onclick="change_month(#Month(t)#)"><h6  class="" >#MonthAsString(Month(t))#</h6></a>
    </cfloop>
</cfoutput>

....but you might need to change the final link.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 37838447
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=#monthNumber#" onclick="change_month(#monthNumber#)">
                            <h6  class="#className#">#MonthAsString(monthNumber)#</h6>
                  </a>
           </cfloop>
           </cfoutput>
0
 
LVL 52

Expert Comment

by:_agx_
ID: 37838458
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">
0
 

Author Comment

by:swaggrK
ID: 37839464
_agx_

The only issue seems to be that the class="active" seems to always default to April, the current month.
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 150 total points
ID: 37839583
That was just me guessing from your original code. But the logic can be anything you want.  For example, if you're linking to the same page, you could use the #url.m_id# value to determine what's "active"

         <!--- use whatever default you want, current month ie #month(now())#, 0, etc... --->
         <cfparam name="url.m_id" default="0">
         ....
         <cfset className =  monthNumber eq url.m_id ? "active" : "normal">
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now