Coldfusion Column Sorting2

I asked this question before...  snakehollywoods answer to my question works, I just found out it doesn't work on my page, that you have to be logged into though, for some reason.  I can get it to work on any other page though except when I have a page protected by this login.  Does anyone else know the answer to the question below, other than a form method?  or a fix? Below is my question 1st, than snakehollywoods answer.


I want to know how to sort the columns of a query.  I know how to do it via the SQL statement.  What
I need is for a user to be able to pull up the webpage with query and click on the column headings of
the table to sort it by that column(asc AND desc would be great...).  For example...  Lets say I have
a table called "music", and I have fields: ID, Artist, Title, Description, and etc.  I want the user
to be able to click on ID to sort the table by ID and if they click on Artist, sort by artist, and etc.
 If there is anyone who can help me on this, it would be greatly appreciated.  

Accepted Answer
From: snakehollywood Date: 09/07/2001 08:18AM PST
Text Below
Question History
Accepted Answer
From: snakehollywood  Date: 09/07/2001 08:18AM PST  
Put buttons into your table headers that will act as the sort button.
When the button is clicked on, it will pass the name of the column to sort into a hidden form field,
and then submit the form back to the same page.


<td><input type="submit" name="sortorder" value="Name" onclick="document.forms[0].orderby.value='name

this puts a button into your column header, which when clicke don, will pass the value "name ASC" into
the hidden form field called orderby.

At the top of the page you simply check for the existance of the form button, if it exists then apply
a dynamic where clause using the hidden formfield.


select * from table
<cfif isdefined('form.sortorder')>
order by #form.orderby#

this will order your query by the column "name" in ascending order.

Now you just create buttons for each column you wish to sort. You can use image buttons for ASC and
DESC options.
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

SouliveConnect With a Mentor Commented:
on each colum heading you make a hyperlink that has a URL variable that travels with it dictating what the query should sort on.  So if your page is displayrecords.cfm and it has columns Artist Titl and Description.  You would make those column titles into links.
<a href="displayrecords.cfm?sort=artist>Artist</a>
<a href="displayrecords.cfm?sort=title>Title</a>
<a href="displayrecords.cfm?sort=label>Record Label</a>

make sure to param the url vars to make them present so you don't have to test for their existance.
<cfparam name="url.sort" default="id">

So your query looks like this.
select *
from music
where 0=0
order by #url.artist#
If you want to put int the ascending/descending feature in there, you could make seperate links for each with the command attached in the url variable.  Since ascending is a default you would have only DESC as a variable.
<a href="displayrecords.cfm?sort=artist>Artist Ascending</a>
<a href="displayrecords.cfm?sort=artist&order=desc>Artist</a>

Set your variables up
<cfparam name="url.sort" default="id">
<cfparam name="url.order" default="">

query looks like this

select *
from music
where 0=0
order by #url.artist# #url.order#

CORRECTION in the first query example:
url.sort not url.artist
bptacekAuthor Commented:
Works perfect!  Thanks!
here is another neat trick you can do too.  Make your column headings images with up and down arrows.  For instance, Artist (up arrow image) and Artist (down arrow image) and then just Artist with no arrow. Call them artistup.gif and artistdown.gif and artistplain.gif.

First make your params and your cfsets.

<cfparam name="url.sort" default="id">
<cfparam name="url.order" default="">

<cfif url.order is "" and url.sort is "artist">
  <cfset url.order="DESC">
  <cfset artistimage="artistdown"
<cfelseif url.order is "DESC" and url.sort is "artist">
  <cfset url.order="">
  <cfset artistimage="artistup"
<cfelseif url.sort neq "artist">
  <cfset url.order="">
  <cfset artistimage="artistplain"

Now on your url you have:

<a href="displayrecords.cfm?sort=artist?order=#url.order#">

Then right after the opening anchor tag you put this:

<img src="#variables.artistimage#.gif">

Then close the link with the </a>

So now each time they click the artist images it will have the appropriate image there to tell you what the colum is doing.  Sort up, sort down, or sorting on something besides artist.

In the future you may even consider sorting on more than one field with the use of cookies or session variables.
All Courses

From novice to tech pro — start learning today.