Link to home
Start Free TrialLog in
Avatar of jabronicus
jabronicus

asked on

Creating a season counter with cfset?

I have created a web page for a company that has seasons.  They will be able to add seasons to their database. The first season is 1. I would like the web application to know that yes this is season 1 next year will be 2 and the year after 3, etc... I am going to put this in my application.cfc so that on pages that need to query the DB i will have a cfset to refer to to call the current season. Is there a way to use a series of cfset to make this work or do I need to be looping?
Avatar of dgrafx
dgrafx
Flag of United States of America image

how about in your controlpanel - create a var that asks what year season 1 is.
So this would then be set as an application var (probably)
(if no controlpanel just hard code it like <cfset Application.seasonone=2005> be sure and lock)
So then whenever you need to know what season it is now - just do code that gets the difference between season 1's year and current year.

like let's say seasonone is 2005
<cfset currentseason=(year(now())-seasonone)+1>
#currentseason#

good luck ...
Avatar of jabronicus
jabronicus

ASKER

I see where you are going.  Thing is when the site is administered from the back-end a season is created in the DB. I need for the web application to know that when date (ie June 1) of each year is reached it is time to switch to the next season. This would be site wide so that I would not have to code individual pages every year to make this happen.

Thanks
One of us is misunderstanding the other ...

1) you said the first year is season 1
so if that same year (is it this year or a year in the past?) is always season 1 then just hard-code it as season1
subsequent years get their season from a simple formula (currentyear - season1year)+1

2) <<This would be site wide so that I would not have to code individual pages every year to make this happen.>>
I'm not sure what you mean by this ??
a) The "not have to code individual pages" part of what you said - You do know that code you put (once) into (or off of) your application cfc or cfm file is available to your entire application.
Like if you put the code I posted into your application.cfc - your can then do #currentseason# on ANY page in your app.
b) The "code individual pages EVERY YEAR" part of what you said - Aren't you having season1 as a fixed year? Season1 doesn't change over time does it? Season1 is the same now as it will be 5 years from now - isn't that correct?

3) I see you added a new variable into the mix "June 1".
Do you need it to switch seasons on June 1?

 You can set the season number based on the current date using this formula..
 
<cfset application.seasonNo = dateDiff("yyyy",'6/1/2005',now()) + 1>

 This will increment every June 1st.  June 1st 2005 thru May 20th 2006 will be season 1, the following year will be season 2.

 The other date in the formula 6.1.2005 should be changed to whatever the start date of the first season was.  This can be hard coded since I assume the date of the first season will never change.

Gotcha! You are correct season1 will always be season1. I would like for the application to switch seasons on a given date.  Here is what I have to this point and it seems to be working minus the switch.


<cfset season1 = 1>
<cfset currentseason = (season1 - season1) +1>
<cfset currentseason = (currentseason - season1) + 1>
another way to do this would simply be to add a bit or boolean field to the db which is called currentseason and then just set that as a flag

then doesn't matter what that actual number of the season is


<cfquery name="myQ"..>
select seasonname from season where currentseason = 2>
</cfquery>


<cfquery name="myQ2"..>
select seasonname from season>
</cfquery>

All the seasons are
<cfoutput query="myQ2">
#myQ2.seasonname#<br>
</cfoutput>
Hey it's season #myQ.seasonname#

btw i do this for a non-profit fund raising site. At the start of each year the volunteer just clicks the start new fund raising season button and it increments the db and sets the season to current...
jabronicus, not sure how that block of code would work.

Does it change on the same date every year or is it a manual change (the person clicks 'start new season') ?

What is the date of the next season change and what season number will it be?


I have tblSeason in database:

-----------------------------------
tblSeason
-----------------------------------
SeasonID    |  SeasonYear
-----------------------------------
    1             |  2006-2007
-----------------------------------
    2             |  2007-2008
-----------------------------------

The admin will create new seasons from the back end. I would like for the season to automatically change June 1 of every year.
ASKER CERTIFIED SOLUTION
Avatar of gdemaria
gdemaria
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
this is my new take on your situation:
now since you have said you want a date where a new season starts you could then do

You'd just include this code into or off of your Application.cfc

This will run the code IF Application.SeasonID is Not defined
<cfif Not StructKeyExists(Application,"SeasonID")>
<cfquery datasource="#dsn#" name="getSeason">
select SeasonID
from tblseason
Where getDate() between Convert(DateTime,SubString(seasonYear,1,CharIndex('-',seasonYear)-1) + '-6-1')
and Convert(DateTime,SubString(seasonYear, CharIndex('-',seasonYear)+1,CharIndex('-',seasonYear)-1) + '-6-1')
</cfquery>
<cflock scope="APPLICATION" type="EXCLUSIVE" timeout="15">
      <cfif isNumeric(getSeason.SeasonID)>
            <cfset Application.SeasonID=getSeason.SeasonID>
      <cfelse>
            <cfset Application.SeasonID=1>
      </cfif>
</cflock>
</cfif>

Then this will run the code IF it is June 1st AND Application.SeasonSet is Not defined
meaning that it will only run once on june 1st
<cfif Not StructKeyExists(Application,"SeasonSet") AND DateFormat(now(),"m/d") is "6/1">
<cfquery datasource="#dsn#" name="getSeason">
select SeasonID
from tblseason
Where getDate() between Convert(DateTime,SubString(seasonYear,1,CharIndex('-',seasonYear)-1) + '-6-1')
and Convert(DateTime,SubString(seasonYear, CharIndex('-',seasonYear)+1,CharIndex('-',seasonYear)-1) + '-6-1')
</cfquery>
<cflock scope="APPLICATION" type="EXCLUSIVE" timeout="15">
      <cfif isNumeric(getSeason.SeasonID)>
            <cfset Application.SeasonID=getSeason.SeasonID>
      <cfelse>
            <cfset Application.SeasonID=1>
      </cfif>
            <cfset Application.SeasonSet=True>
</cflock>
</cfif>