SfC Home > Web Design > SQL > ColdFusion >
Explanation of Sorting a Table Listing in ColdFusion. Key words: database, SQL, query, ascend, descend, order, variable, <CFIF>, <CFELSE>, tags, IsDefined, URL, Adobe, 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.
Questions you may have include:
- What is an example of resorting?
- What is the code used?
This lesson will answer those questions.
Example of resorting
Name(sort descending) |
|
5 |
Carbohydrates |
4 |
Calorie |
3 |
Buffer solution |
2 |
British thermal unit |
1 |
Breaker |
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.
See the Side Menu for more ColdFusion lessons
Keep code simple
Resources and references
The following resources provide information on this subject.
Websites
Books
What do you think?
Do you have any questions, comments, or opinions on this subject? If so, send an email with your feedback. I will try to get back to you as soon as possible.
Share link
Click on a button to send an email, Facebook message, Tweet, or other message to share the link for this page:
Students and researchers
The Web address of this page is:
www.school-for-champions.com/coldfusion/sort_table.htm
Please include it as a link on your website or as a reference in your report, document, or thesis.
Where are you now?
Sorting a Table Listing in ColdFusion
