Solved

Creating a season counter with cfset?

Posted on 2007-04-03
11
315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

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…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

627 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