URL Management
How to Organize URL Paramenters for Easy Management

Many people use Coldfusion Studio and Homesite for web development. Both have a feature that lets the user view the right margin. I set my right margin at 91 for a specific reason. I knew that all code before 91 would not wrap on print. This made it easier for others and myself to read printed code. One thing kept foiling my attempts to keep my code "pretty": Long urls with a lot of parameters. The method I started using to keep my code presentable turned out to be very useful in other areas.

It is not uncommon for a query to return a set of data that will be looped through in order to display an item and link it to more detailed information about the item. Sometimes my links would have more inforation than the item identifier (we routinely pass a GUID on every URL). I realize that most programmers will not pass too much information by the URL so view the following link as an exteme example (I did not tack on the GUID either):

	<a href="url.cfm?Firstname=John&Lastname=Doe&Age=28&HeightFT=5&HeightIn=7&Weight=135">Go</a>

In order to make this more manageable, I started using cfscript to build the URL string.

    <cfscript>
        u = "Firstname=#urlencodedformat(encrypt(Firstname,'555'))#&";
        u = u & "Lastname=#Lastname#&";
        u = u & "Age=#Age#&";
        u = u & "HeightFT=#HeightFT#&";
        u = u & "HeightIn=#HeightIn#&";
        u = u & "Weight=#Weight#&";
        //u = u & "GUID=#GUID#&";
    </cfscript>
    
    <a href="url.cfm?#u#">Go</a>

This is the same link presented above with two exceptions. The paramater Firstname as been encoded. Next, a parament named GUID is present on the last line. It has been commented out. As the example shows, it is pretty easy to secure a url parameter in this format. It is also easy to turn a parameter off. Finally, it is easy to add more paramaters; just add another line.

The following code can be cut from this document, then pasted into a Coldfusion template. When done, it should run immediately without modification.

	<!--- This demonstrates how I use cfscript to build my url strings when they get --->
	<!--- long.  This breaks the url down into smaller components that I believe are --->
	<!--- easier to manage. --->

	<cfparam name="NewRec" default="No" />
	<cfparam name="GUID" default="Test" />

	<cfoutput>

		<!--- Build the information table and the link --->
		<cfif NewRec is "No">
			<!--- Make a list of values to display --->
            <cfscript>
				lstValues=            "John,Doe,30,6,2,200|";
				lstValues=lstValues & "Jane,Doe,28,5,7,135|";
				lstValues=lstValues & "Joe,Smith,35,5,9,225|";
				lstValues=lstValues & "Jane,Smith,36,5,2,140|";
			</cfscript>
			
            <!--- Convert the list to a query in order to make the example more realistic. --->
			<cfset qryPatients = QueryNew("FirstName,LastName,Age,HeightFt,HeightIn,Weight") />
			
            <cfloop list="#lstValues#" index="i" delimiters="|">
				<cfset temp = QueryAddRow(qryPatients) />
                <cfset Temp = QuerySetCell(qryPatients, "FirstName", listgetat(i,1)) />
                <cfset Temp = QuerySetCell(qryPatients, "LastName",  listgetat(i,2)) />
                <cfset Temp = QuerySetCell(qryPatients, "Age",       listgetat(i,3)) />
                <cfset Temp = QuerySetCell(qryPatients, "HeightFt",  listgetat(i,4)) />
                <cfset Temp = QuerySetCell(qryPatients, "HeightIn",  listgetat(i,5)) />
                <cfset Temp = QuerySetCell(qryPatients, "Weight",    listgetat(i,6)) />
			</cfloop>

			<!--- Display the data --->
			<div align="center">
			<table border="1">
				<tr>
					<td>First</td>
                    <td>Last</td>
                    <td>Age</td>
                    <td>Height</td>
					<td>Weight</td>
					<td> </td>
				</tr>
				<cfloop query="qryPatients">
				<tr>
					<td>#Firstname#</td>
                    <td>#Lastname#</td>
                    <td>#Age#</td>
                    <td>#HeightFt#'#HeightIn#"</td>
                    <td>#Weight#</td>
                    <!--- Build the URL string with CFScript so that it is easier to read and manage. --->
                    <!--- Notice how GUID is commented out.  Anytime I want to put it back into the --->
                    <!--- string, I just have to remove //.  --->
                    <!--- This method could also be used with the Encrypt and decrypt functions --->
                    <!--- in order to make URL parameters a little safer.  See firstname below. --->
                    <cfscript>      
					  u =     "Firstname=#urlencodedformat(encrypt(Firstname,'555'))#&";
					  u = u & "Lastname=#Lastname#&";
					  u = u & "Age=#Age#&";
					  u = u & "HeightFT=#HeightFT#&";
					  u = u & "HeightIn=#HeightIn#&";
					  u = u & "Weight=#Weight#&";
					  //u = u & "GUID=#GUID#&";
					  u = u & "NewRec=yes";
				   </cfscript>
				   <td><a href="url.cfm?#u#">Go</a></td>
				</tr>
			</cfloop>
			</table>
		</div>

		<!--- Display the information off of the URL                                           --->
	<cfelse>
   		<cfloop item="i" collection="#URL#">
	   		<br />#i#-#evaluate("#i#")#
   		</cfloop>
   		<cfset strHold=urldecode(decrypt(firstname,'555')) />
  		<br />Decoded String:  #strHold#
	</cfif>
</cfoutput>

Disclaimer: The terms "Best Practice" and "Coding Standards" are subjective. This means that what is a "best practice" or a "standard" for one may be a pox for another. I am in no way claiming that anyone should use the above code. It is just an example of something that I find useful.

As far as I know, this work is original. I have not intentionally copied anyone's work.

About This Tutorial
Author: Jerry Barnes
Skill Level: Beginner 
 
 
 
Platforms Tested: CF5,CFMX
Total Views: 36,195
Submission Date: September 09, 2004
Last Update Date: August 25, 2011
All Tutorials By This Autor: 4
Discuss This Tutorial
  • Thanks for the tutotial. http://pass.nejcpass.com

Advertisement

Sponsored By...
Powered By...