Solved

Creating a season counter with cfset?

Posted on 2007-04-03
11
307 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
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.

 
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
 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A web service (http://en.wikipedia.org/wiki/Web_service) is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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