Solved

Creating a season counter with cfset?

Posted on 2007-04-03
11
298 Views
Last Modified: 2013-12-24
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?
0
Comment
Question by:jabronicus
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 25

Expert Comment

by:dgrafx
ID: 18847867
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 ...
0
 

Author Comment

by:jabronicus
ID: 18849925
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
0
 
LVL 25

Expert Comment

by:dgrafx
ID: 18850198
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?
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 18850397

 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.

0
 

Author Comment

by:jabronicus
ID: 18850427
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>
0
Scale it in WD Gold

With up to ten times the workload capacity of desktop drives, WD Gold hard drives employ advanced technology to deliver among the best in reliability, capacity, power efficiency and performance.

 
LVL 36

Expert Comment

by:SidFishes
ID: 18850462
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#

0
 
LVL 36

Expert Comment

by:SidFishes
ID: 18850487
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...
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 18850949
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?


0
 

Author Comment

by:jabronicus
ID: 18851567
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.
0
 
LVL 39

Accepted Solution

by:
gdemaria earned 500 total points
ID: 18851713

 So it looks like the very first season started in 2006...

<cfset application.seasonNo = dateDiff("yyyy",'6/1/2006',now()) + 1>

This will workk as long as the database assigns the numbers consistently and sequentially.  Then you will be in sync with them.


 What would be really accurate is to add a simple field to the database table "startDate" and then do a query..

<cfif NOT isDefined('application.seasonNo')>
<cfquery name="getSeason" ...>
 select max(seasonID) as SeasonID
 from tlbSeason
 where startDate < getDate()
</cfquery>

  <cfset application.seasonNo = getSeason.seasonID>
</cfif>
0
 
LVL 25

Expert Comment

by:dgrafx
ID: 18852125
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>
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

12 Experts available now in Live!

Get 1:1 Help Now