Tip of the Day


There used to be a tutorial on how to make a tip of the day program here. I think
it was lost when the server got hacked. I thought I would make a new one. This
can be accomplished in many ways but here is the solution that I found.

Database Set up
I prefer to use MySQL but any database will work. I made 2 tables. The 1st table stores
the Tip of the Day information and has the following columns.

Table: tipoftheday
Field: totd_ID, Type: tinyint(4), Attribute: auto_increment, Primary Key
Field: totd, Type: Text

Table: totd_day_randomnum
Field: date, Type: date
Field: random_number, Type: tinyint(4)

The key is the totd_day_randomnum Table does NOT have a primary key. It is going to be overwritten
every time the day changes.

This set up allows for 2 versions. The 1st version will only update the Tip of the Day on a daily
basis. The 2nd will update the Tip of the Day every time the page is refreshed.

Ok, let's get started.
------------------------------------------------------------------------------------------------------
This 1st part is a little administrative information about the database you are using and your server.

<!--- Creates variables that stores the database information --->
<!--- Datasource --->
<cfset ds = "tutorials">
<!--- Username if your host requires it, otherwise leave this blank. --->
<cfset un = "">
<!--- Password if your host requires it, otherwise leave this blank. --->
<cfset pw = "">

Now we are going to setup the variable that will allow you to keep the same Tip of the Day all day or
change it on page refresh.

<!--- Creates a variable that stores if user wants totd everyday or whenever the screen is refreshed --->
<!--- Set to 1 to keep same totd all day. Set to 0 to change totd whenever screen is refreshed. --->
<cfparam name = "everyday" default = "1">

Simple query so we know how many tips there are. This will be used in our random number generation
so that you won't have to hard code it.

<!--- Query to set up minumum and maximum rows for recordcount data. --->
<cfquery name="all" datasource="#ds#" username="#un#" password="#pw#">
    SELECT totd_ID, totd
    FROM tipoftheday
</cfquery>

<!--- Set the number of tips to cycle through --->
<cfset StartRow = "1">
<cfset MaxRows = "#all.RecordCount#">

Now we create our random number.

<!--- Creates a variable that stores the random # for totd_ID --->
<cfparam name = "RandomNumber" default = "#RandRange(StartRow, MaxRows)#">

The bulk of the code starting out with a cfif to determine all day or on refresh status.

<!--- If user wants totd to last whole day. --->
<cfif Trim(everyday) is "1">
    <cfquery name="AllDayNumber" datasource="#ds#" username="#un#" password="#pw#">
        SELECT *
        FROM totd_day_randomnum, tipoftheday
    </cfquery>
    <cfset AllDayRandom ="#AllDayNumber.random_number#">
    <cfquery name="AllDay" datasource="#ds#" username="#un#" password="#pw#">
        SELECT *
        FROM totd_day_randomnum, tipoftheday
        WHERE totd_ID = <cfqueryparam value="#AllDayRandom#" cfsqltype="CF_SQL_INTEGER">
    </cfquery>

This part of the code checks to see if the stored database date is different than the current date.
If the current date is not the same as the stored date, the query updates the database with the current
date and then a new random number. Then it displays the new Tip of the Day. However, if the current
stored date is the same it just defaults to the cfelse block and shows the Tip of the Day.
    
    <cfif #Abs(DateDiff("d", "#date#", "#now()#"))# NEQ "0">
        <!--- Set up the query that will change the Random Number based on the day. --->
        <cfquery name="UpdateDateRandomNum" datasource="#ds#" username="#un#" password="#pw#">
            UPDATE totd_day_randomnum
            SET date = #CreateODBCDate(now())#,
            random_number = #RandRange(StartRow, MaxRows)#
        </cfquery>
        <cfset AllDayRandom ="#AllDayNumber.random_number#">
        <cfoutput query="AllDay">
            <tr align="left"><font color="Blue" size=""><b>#totd#</b></font></tr><br>
        </cfoutput>
    <cfelse>
        <cfoutput query="AllDay">
            <tr align="left"><font color="Blue" size=""><b>#totd#</b></font></tr><br>
        </cfoutput>
    </cfif>
</cfif>


This section is for people that want the Tip of the Day to refresh with each page load.

<!--- If user wants totd to reset when page is refreshed. --->    
<!--- Creates a variable that stores today's date --->
<cfparam name = "today" default = "#CreateODBCDate(now())#">
<!--- Set up the actual query that will display the appropriate tip of the day. --->
<cfquery name="display" datasource="#ds#" username="#un#" password="#pw#">
    SELECT totd_ID, totd
    FROM tipoftheday
    WHERE totd_ID = <cfqueryparam value="#RandomNumber#" cfsqltype="CF_SQL_INTEGER">
</cfquery>

<cfif Trim(everyday) is "0">
    <cfoutput query="display">
        <tr align="left"><font color="Blue" size=""><b>#totd#</b></font></tr><br>
    </cfoutput>
</cfif>    

Example of the Application: http://migcfdesign.com/Junk/datetest.cfm
Final code without all the comments:
<!--- Creates variables that stores the database information --->
<!--- Datasource --->
<cfset ds = "tutorials">
<!--- Username if your host requires it, otherwise leave this blank. --->
<cfset un = "mgomez">
<!--- Password if your host requires it, otherwise leave this blank. --->
<cfset pw = "hunkydory">

<!--- Creates a variable that stores if user wants totd everyday or whenever the screen is refreshed --->
<!--- Set to 1 to keep same totd all day. Set to 0 to change totd whenever screen is refreshed. --->
<cfparam name = "everyday" default = "1">

<!--- Query to set up minumum and maximum rows for recordcount data. --->
<cfquery name="all" datasource="#ds#" username="#un#" password="#pw#">
SELECT totd_ID, totd
FROM tipoftheday
</cfquery>

<!--- Set the number of tips to cycle through --->
<cfset StartRow = "1">
<cfset MaxRows = "#all.RecordCount#">

<!--- Creates a variable that stores the random # for totd_ID --->
<cfparam name = "RandomNumber" default = "#RandRange(StartRow, MaxRows)#">

<!--- If user wants totd to last whole day. --->
<cfif Trim(everyday) is "1">
<cfquery name="AllDayNumber" datasource="#ds#" username="#un#" password="#pw#">
SELECT *
FROM totd_day_randomnum, tipoftheday
</cfquery>
<cfset AllDayRandom ="#AllDayNumber.random_number#">
<cfquery name="AllDay" datasource="#ds#" username="#un#" password="#pw#">
SELECT *
FROM totd_day_randomnum, tipoftheday
WHERE totd_ID = <cfqueryparam value="#AllDayRandom#" cfsqltype="CF_SQL_INTEGER">
</cfquery>
<cfif #Abs(DateDiff("d", "#AllDay.date#", "#now()#"))# NEQ 0>
<!--- Set up the query that will change the Random Number based on the day. --->
<cfquery name="UpdateDateRandomNum" datasource="#ds#" username="#un#" password="#pw#">
UPDATE totd_day_randomnum
SET date = #CreateODBCDate(now())#,
random_number = #RandRange(StartRow, MaxRows)#
</cfquery>
<cfset AllDayRandom ="#AllDayNumber.random_number#">
<cfoutput query="AllDay">
<tr align="left"><font color="Blue" size=""><b>#totd#</b></font></tr><br>
            (Here is some database information just to show what is going on behind the scenes.<br>
            There are #all.RecordCount# tips in total! This is tip #AllDayRandom#... The current time is #now()# The stored date is #date# <br>
            The date difference is #Abs(DateDiff("d", "#date#", "#now()#"))#)
</cfoutput>
<cfelse>
<cfoutput query="AllDay">
<tr align="left"><font color="Blue" size=""><b>#totd#</b></font></tr><br>
            (Here is some database information just to show what is going on behind the scenes.<br>
            There are #all.RecordCount# tips in total! This is tip #AllDayRandom#... The current time is #now()# The stored date is #date# <br>
            The date difference is #Abs(DateDiff("d", "#date#", "#now()#"))#)
</cfoutput>
</cfif>
</cfif>
<p>
Here you can see that if you refresh the page, the Tip of the Day changes.<br>
<!--- If user wants totd to reset when page is refreshed. --->
<!--- Creates a variable that stores today's date --->
<cfparam name = "today" default = "#CreateODBCDate(now())#">
<!--- Set up the actual query that will display the appropriate tip of the day. --->
<cfquery name="display" datasource="#ds#" username="#un#" password="#pw#">
SELECT totd_ID, totd
FROM tipoftheday
WHERE totd_ID = <cfqueryparam value="#RandomNumber#" cfsqltype="CF_SQL_INTEGER">
</cfquery>

<cfif Trim(everyday) is "0">
<cfoutput query="display">
<tr align="left"><font color="Blue" size=""><b>#totd#</b></font></tr><br>
</cfoutput>
</cfif>

All ColdFusion Tutorials By Author: Michael Gomez
  • Tip of the Day
    Simple page to display a Tip of the Day, either all day or randomly.
    Author: Michael Gomez
    Views: 4,455
    Posted Date: Saturday, February 7, 2009
Download the EasyCFM.COM Browser Toolbar!