Dynamic Sorting

<!--- First off we need to set a default sort order so for the purposes of this
tutorial we will use fname as our default. We set this using the cfparam tag --->


<cfparam name="sortBy" default="fname">

<!--- Next thing we need to do is make our call to our database using the cfquery tag --->
<!--- In this example I have used the name of my company as the datasource name you would
of course use the datasource name you have setup --->

<cfquery name="dynamicSort" datasource="rez_productions">
   SELECT id, fname, lname
   FROM sorting 
   <!--- Note: the field we are sorting by is a dynamic variable which we set with our cfparam tag --->
   ORDER BY #sortBy# Asc 
</cfquery>

<!--- It is easier for me to give you the entire page layout for formating purposes. --->
<html>
<head>
<title>
Dynamic Sorting</title>
<meta http-equiv=
"Content-Type" content="text/html; charset=iso-8859-1">
</head>


<body>

<table width="500" border="1" align="center">
  <tr>
    <td>

    <div align="center">
    <!--- Note the links all reference the page we are on, yet they are passing the field name
           that we want our end-user to be able to click if they want to sort by this particular field.
           All three links are basically the same the only change is in the value that they are passing --->

      <a href="dynamic_sort.cfm?sortBy=ID">Id</a>
    </div>
   
</td> 
    <td align=
"left">
    <div align="left">
      <a href="dynamic_sort.cfm?sortBy=fname">FirstName</a>
    </div>
    </td> 
    <td>

    <div align="left">
       <a href="dynamic_sort.cfm?sortBy=lname">Last Name</a>
    </div>
    </td> 
  </tr>

     <!--- Now we do our output. It is important to place our cfoutput tag in the proper place
             so that we can dynamically generate the needed rows for the amount of data we are displaying.
             Because we do not know how many rows we need we place the cfoutput tag before the opening <tr> --->

  <cfoutput query="dynamicSort">
  <tr> 
     <td>
<div align="center">#dynamicSort.id#</div></td>
     <td>
#dynamicSort.fname#</td>
     <td>
#dynamicSort.lname#</td> 
  </tr> 

  <!--- Note that the closing cfoutput tag is placed after the closing </tr> tag this is part of
          dynamically generating rows --->

  </cfoutput> 
</table>
</body>
</html>


Thats pretty well it for basic dynamic sorting. Here is the MySql Database I used to create
this tutorial.

CREATE TABLE sorting (
        id int(10) unsigned NOT NULL auto_increment,
        fname varchar(255) NOT NULL,
        lname varchar(255) NOT NULL,
        PRIMARY KEY (id)
);

#
# Dumping data for table 'sorting'
#

INSERT INTO sorting VALUES ( '1', 'Bugs', 'Bunny');
INSERT INTO sorting VALUES ( '2', 'Road', 'Runner');
INSERT INTO sorting VALUES ( '3', 'Daffy', 'Duck');

All ColdFusion Tutorials By Author: Redmanz
  • Send Page Url to A Friend
    A simple script where a user can click a link that will take the current templates url and pass it to a email form which they can than fill in with their friends emails addresses to send their friends your site address.
    Author: Redmanz
    Views: 39,195
    Posted Date: Saturday, November 23, 2002
  • Advanced Form Checking
    This tutorial places the submitted form values into a session, than error checks them. If there is an error it will show the form again with the previous values the user entered pre-entered in the form. It will also tell the user which form fields had errors in them.
    Author: Redmanz
    Views: 53,958
    Posted Date: Tuesday, November 26, 2002
  • Dynamic Sorting
    This tutorial shows the basics of allowing your end-users to dynamically sort the order in which they view your records. You can have as many sort orders as you have fields viewed!
    Author: Redmanz
    Views: 34,213
    Posted Date: Thursday, December 5, 2002
  • Duplicate Data Checking
    This tutorial uses a opt-in mailing list as an example. After the end-user enters their information the information goes to an action page which checks to see if the users email address already exists in the database. If it exists it lets them know and will not enter the duplicate data, if it does not exist it enters them into the mailing list.
    Author: Redmanz
    Views: 36,916
    Posted Date: Monday, December 9, 2002
  • Guest Book (Part 1 / 2)
    This Guestbook is part one of a two part tutorial. This part deals with the guest book itself the second part will build a administration area for this guestbook. The guest book submits to two tables in a mySql database the first table being for the actual guest book display template and the second table to be used for an opt-in mailing list.
    Author: Redmanz
    Views: 51,764
    Posted Date: Friday, December 13, 2002
  • Guest Book (Part 2 / 2)
    This is part two of my guestbook tutorial, in part one we created the actual guestbook. In this part we will now look at creating an administration area for our guestbook. In this admin area we will be able to edit and delete guestbook entries as well as add or delete administrators.
    Author: Redmanz
    Views: 34,282
    Posted Date: Saturday, December 21, 2002
  • Show Source to Friends
    Not really a tutorial but some code to help you solve programming problems by showing your code online to your friends or associates.
    Author: Redmanz
    Views: 25,576
    Posted Date: Tuesday, February 18, 2003
Download the EasyCFM.COM Browser Toolbar!