Solved

Creating a season counter with cfset?

Posted on 2007-04-03
11
303 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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 showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

816 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