Showcase and discover digital art at yex

Follow Design Stacks

Subscribe to our free newsletter to get all our latest tutorials and articles delivered directly to your inbox!

ColdFusion Tutorial – More Template Building

Page 1 — More Template Building

In Lesson 1 and Lesson 2 of this tutorial, we used ColdFusion to display a database that contained a definitive record of the content of my pockets. As fascinating as these items may be, I feel it’s time to move on to a new hypothetical database so that I can demonstrate some of ColdFusion’s sorting functions. We’ll assume that I have a second database called links_to_bad_sites.mdb and that we’ve already defined it as the DSN links_to_bad_sites in ColdFusion Administrator.

Within this database, there is a single table called Links. It’s the product of hours of research and contains the names, URLs, and descriptions of really bad Web sites I’ve turned up. There’s also a field that indicates the date the record was added to our database.

Now let’s complicate matters further by assuming that we’re writing a template for a What’s New page on our site. We’d probably name it whats_new.cfm or – for increased job security – aAEr348u234.cfm.

We want our What’s New page to list only the links that we’ve added in December, and we want them to appear on our page in alphabetical order by site name. Our query statement would look something like this:

<CFQUERY NAME= "new_links" DATASOURCE= "links_to_bad_sites">
SELECT * FROM links WHERE date IS LIKE "December%" ORDER BY name ASC

You should already be familiar with everything in this query statement except the ORDER BY part. This defines the field we want to sort by and specifies whether we want the results in ascending order (ASC) or descending order (DESC). If you don’t put ASC or DESC, ColdFusion will default to ASC.

Now that we’ve set up a query, the CFOUTPUT section of our What’s New page might look like this:


<H3>Really Bad Sites Found in December</H3>

<CFOUTPUT QUERY= "new_links">

Site Name: #Name# <BR>
URL: <A HREF="#URL#">#URL#</A> <BR>
Description: #Description# <P>


See how damn clever we are? We used the #URL# variable twice so that we get the text of the URL and also make a live link to the site.

Let’s take it a step further and assume that we only want to output two links from the month of December, just two tiny morsels to whet the appetite. We could use CF’s MAXROWS specification within our CFOUTPUT tag, like this …

<CFOUTPUT QUERY= "new_links" MAXROWS="2">

… and the rest of the CFOUTPUT statement would remain the same. The code above would return a page that looks like this:

Two Really Bad Sites Found in December

Site Name: Ideagirl’s Homepage
Description: Ideagirl must have really studied the list of “Top Ten Things NOT to Do on Your Web Site,” because she takes great pains to use all of them here: blinking text, unreadable text colors, loads of animated GIFs stolen from other sites, MIDI files on every page, and lots of endless blathering about nothing at all. We recommend it highly.

Site Name: EverythingBlows.Com
URL: http://www.everythingblows .com
Description: These curmedgeons seem to be angry about everything they’ve ever seen, touched, or tasted in the world. They’re the sort of guys you’d find huddled in the corner at a party, pointing out the fashion faux pas and false mannerisms of the other guests. Highly recommended.

Now you know how to select, sort, format, and limit data in ColdFusion templates. But before you run off and start bragging in the bars, I should tell you that you’ve only scratched the surface. Let’s move on to some handy tips for veteran loafers.