CFTIME: How to Make Content Appear and Disappear

Content on the web often has a shelf life. You may have information that you want up after a certain point in time or you may want it to be gone after a certain time.You may want to have it appear and disappear without having to be editing files or changing out input in some back end system. You will find that you can build a simple system for publishing and removing info with ColdFusion's CFIF tag and CreateDateTime() and Now() functions.

CFIF is one of the most basic and commonly used tags in ColdFusion. We will use it to hide or display content based on comparing a set date to the time that the page is parsed for the visitor.

The CreateDateTime() function takes date and time parameters and creates a ColdFusion date/time object. The format is CreateDateTime(year,month, day, hour, minute, second). An example would be CreateDateTime (2004, 7, 9,19,59, 0) for July 9, 2004 at 7:59pm.

The Now() function takes no parameters and produces a ColdFusion date/time object for the current date and time including seconds.

Example Code

The components are simple so let's put it all together. Our first example is content that displays until a future point in time. In this case if the date/time is less than the target date your content will display. This would be usefull if yo had a contest running and wanted to remove the content from your site when the contest was over.

<cfif #now()# lte #CreateDateTime (2004, 7, 9,19,59, 0)#>
This content is displayed until July 7, 2004 at 7:59pm.

</cfif>

Our next example is content that displays after a future point in time. In this case if the date/time is greater than the target date your content will display. This would be handy if you had an announcement coming up so you could announce it on your web site without having to sit at your computer and trigger the new content.

<cfif #now()# gte #CreateDateTime (2004,7,5,10,30, 0)# >
This content is displayed after July 5, 2004 at 10:30am.
</cfif>

Our last example is content that displays after a future point in time and ends at a later date. In this case if the date/time is greater than the target date and it was smaller than a second target date, your content will display. This would be handy if you had an announcement coming up so you could announce it on your web site without having to sit at your computer and trigger the new content and it would also remove the announcement at the appropriate time.

<cfif #now()# lte #CreateDateTime (2004, 7, 9,19,59, 0)#
AND #now()# gte #CreateDateTime (2004,5, 2,10,30, 0)# >
This content is displayed after May 5, 2004 at 10:30am and disappears after July 7, 2004 at 7:59pm .
</cfif>

Summary

ColdFusion makes dealing with times and dates easy with built in functions like CreateDateTime() and Now(). Developers won't need to be on call for special announcements or the end of a campaign. Test out some examples of your own making and then don't let the clock and your computer dictate when you are making content appear and disappear.

Grouping Results with CFQUERY and CFOUTPUT

Grouping Results

ColdFusion makes it easy to select data out of a database and display it on a page. As soon as you feel good about your success in crossing the divide from static to dynamic, someone will say "That's nice, can you group the list by author?". Whether you are asked to group things by author, make, brand, year or whatever, ColdFusion has you covered with its grouping capabilities.

To present things in groups, we first rely on SQL to group the data before it is presented to ColdFusion. This is done with a GROUP BY or ORDER BY clause.

CLAUSE QUERY
GROUP BYSELECT Author, Title, Publisher
GROUP BY Publisher, Author, Title
ORDER BY SELECT Author, Title, Publisher
ORDER BY Publisher, Author, Title

ColdFusion will then display grouped data by using nested CFOUTPUT tags. In both cases the outter most CFOUTPUT will indicate the QUERY and each CFOUTPUT that is used to group will use a GROUP parameter except the innermost CFOUTPUT. The code below will group authors by publisher and then group books by author.

CODE
<CFOUTPUT Query="myquery" GROUP="Publisher">
  <p>#Publisher#
  <CFOUTPUT GROUP="Author">
    <br>#Author#
    <CFOUTPUT>
   <br>~ #Title#
    </CFOUTPUT>
  </CFOUTPUT>
</p>
</CFOUTPUT>
OUTPUT

Macromedia Press
Forta
~CF WACK Third Edition

~Reality ColdFusion MX

Newbie Press
Barr
~Instant Messenger for Dummies
Gates
~Dominating the World with Software Patches

If you use a GROUP BY clause with a WHERE clause the GROUP BY clause most come after the WHERE clause. Think of this as the SQL engine finding all of the results that match the WHERE statement and then it groups the results.

If you use a GROUP BY clause with an ORDER BY, use the GROUP BY before the ORDER BY clause. The SQL engine will create the groups and then order the groups, this reduces the amount of ordering that is done by SQL since the number of groups is the same or less than the number of members of the groups.

BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.