The School for Champions is an educational website that shows you how to achieve your dreams.



Other ColdFusion topics:

Basics

Overview of ColdFusion

ColdFusion Features

Development methods

Setting up ColdFusion

ColdFusion Development Steps

Specific applications

Using Session Variables

Combining the Form and Action Pages

Using Drop-Down Lists

Converting ColdFusion to PDF

Dealing with Dates and Times

Sending a Flash Animation with ColdFusion Email

Changing Data to Telephone Format

Populating a New Database Table from an Old Table

Populating a Matrix Table

Sorting a Table Listing in ColdFusion

Also see:

Weekly Feedback Blog

ColdFusion Survey Results

Flash Development

Website Development

eLearning


SfC Home > ColdFusion >

Explanation of how to sort a table listing in Macromedia ColdFusion. Also refer to database, SQL, query, ascend, descend, order, variable, <CFIF>, <CFELSE>, tags, IsDefined, URL, Ron Kurtus, School for Champions. Copyright © Restrictions

Sorting a Table Listing in ColdFusion

by Ron Kurtus (5 January 2003)

When you populate a table with items from a database query, using Macromedia ColdFusion, you can list items in ascending or descending order with the ORDER BY command in SQL. But you may also want to allow the user to resort a specific column in the table.

This lesson will answer those questions. There is a mini-quiz near the end of the lesson.

Example of resorting

Number

Name (sort descending)

1

Breaker

2

British thermal unit

3

Buffer solution

4

Calorie

5

Carbohydrates

The table above has been populated from a database query. If you click on the Number item in the heading, you can resort the listings. Clicking on Word will resort in a descending order.

Code

The code for sorting the table list is relatively simple. Clicking on a heading item sends a variable to a set of <CFIF>... <CFELSE>... </CFIF> CFML tags on the same page, where the initial query and the sorting query exist. Then the table is populated from <CFOUTPUT>.

Query

The <CFIF> tag shows its contents if the url.sorter variable has been sent.

<CFIF IsDefined("url.sorter")>

Then the query will be ordered by the value of url.sorter that is sent.

<CFQUERY NAME="resort" DATASOURCE="xxx">
SELECT numb, name FROM Table_Name
ORDER BY #url.sorter#
</CFQUERY>

If the variable has not been sent (like in the initial view of the page), we see the other query.

<CFELSE>

<CFQUERY NAME="resort" DATASOURCE="xxx">
SELECT numb, name FROM Table_Name
</CFQUERY>

</CFIF>

Table

The variable sorter is sent by clicking on a link. The result of sending the first variable will result in an ascending sort. The second variable sorter=word DESC will provide a descending sort.

<TABLE>
<TR>
<TD><A HREF="this_page.cfm?sorter=numb">Number</A></TD>
<TD><A HREF="this_page.cfm?sorter=word DESC">Name</A></TD>
</TR>
<CFOUTPUT QUERY="resort">
<TR>
<TD><P>#numb#</P></TD>
<TD><P>#name#</P></TD>
</TR>
</CFOUTPUT>
</TABLE>

Summary

You can populate a table with items from a database query, and then sort according selected items in the heading using simple ColdFusion code.

Answers to Readers' Questions


Keep code simple


Resources

The following resources provide information on this subject.

Websites

ColdFusion Resources

Books

Top-rated books on ColdFusion


Mini-quiz to check your understanding

1. What is ColdFusion Studio?

An image tool studio-quality pictures for the Web

An add-on to generate custom tags

A mark-up editor for building Web sites

2. What is needed to display ColdFusion pages on the Web?

Users must own ColdFusion

A ColdFusion server is needed

HTML

3. What is a major application of ColdFusion?

Dynamic, data-driven pages

Animation databases

Sites that cannot be hacked

If you got all three correct, you are on your way to becoming a Champion in ColdFusion Development. If you had problems, you had better look over the material again.


What do you think?

Do you have any questions, comments, or opinions on this subject? If so, send an email with your feedback. We will try to get back to you as soon as possible.


Share link

Feel free to establish a link from your website to pages in this site.

Or use our form to send this link to yourself or a friend.


Students and researchers

The Web address of this page is
www.school-for-champions.com/coldfusion/sort_table.htm.

Please include it as a reference in your report, document, or thesis.


Where can you go from here?

School for Champions

ColdFusion topics

Sorting a Table Listing in ColdFusion


The School for Champions helps you become the type of person who can be called a Champion.