I had problems in retyping the urls variables over and over again so ive decided to create a function that can replace the value of onlyone variable or more according to my needs.
This function is also found in my "Coldfusion DBL Framework System"

Adds or replaces a variable with its value on a URL query string

A string formated as a "cgi.query_string" value. eg. "col=t1&yo=tab&me=2"

Function syntax
QueryStringSet(querystring, variable(s) list, value(s) list)

<h3>QueryStringSet Example</h3>
<!--- Getting the current URL query string --->
<cfset oldQueryString = CGI.QUERY_STRING>
<!--- Adding a new variable and replacing a value to the string --->
<cfset newQueryString = QueryStringSet(oldQueryString,'test1,test2','val1,val2')>
<!--- Printing out the results --->
    Source Query String : #oldQueryString#<br>
    Formated Query String : #newQueryString#

Browser Output
Source Query String : fusebox=sql&test1=test&db=b2evolution
Formated Query String : fusebox=sql&test1=val1&db=b2evolution&test2=val2


<!--- *********************************************************************
* Function: QueryStringSet *
* Arguments: QueryString,Variable,Value *
* Replace or adds a variable with its value on a querystring *
********************************************************************* --->

<cffunction name="QueryStringSet" access="public" hint="Looks for the variable name on the querystring if exists replaces its value, if not adds a newone">
   <!--- Arguments --->
   <cfargument name="QueryString" type="string" required="true" hint="holds the QueryString">
   <cfargument name=
"Variable" type="string" required="true" hint="holds the variable name">
   <cfargument name=
"Value" type="string" required="true" hint="holds the value to be replace or added">
   <cfset pointers = '>
<!--- Holds the pointers so we know where to look to replace the variable --->
   <cfloop from=
"1" to="#listlen(arguments.variable)#" index="vCount">
      <!--- Loop thru the variable names if there is more than one separated by commas --->
      <cfloop from="1" to="#listlen(arguments.QueryString,'&')#" index="count">
         <!--- Loop thru the QueryString values to make it easier to find the variable names --->
 <cfif find(listgetat(arguments.variable,vCount),listgetat(listgetat(arguments.querystring,count,'&'),1,'=')) GT 0>
            <!--- Lets check if while looping we can find a variable that matches our request --->
            <cfset pointers = listappend(pointers,count)>
            <!--- If we do find one lets append the position number to a list --->
      <cfif listlen(pointers)
gt 0>
<!--- If we found at least one or more variables that match our request --->
         <cfloop list="#pointers#" index="point">
            <!--- Lets loop thru the pointer list to replace the variable with the new value --->
            <cfif listgetat(listgetat(arguments.querystring,point,
'&'),1,'=') eq listgetat(arguments.variable,vCount)>
               <!--- Checking if the variable from the querystring matches our request just to make sure --->
               <cfset arguments.querystring = listsetat(arguments.querystring,point,listgetat(arguments.variable,vCount) & '=' & listgetat(arguments.value,vCount),'&')>
               <!--- Lets replace our variable value --->
         <cfset arguments.querystring = listappend(arguments.querystring,listgetat(arguments.variable,vCount) &
'=' & listgetat(arguments.value,vCount),'&')>
         <!--- If not variable found in querystring lets add it --->
   <cfreturn arguments.querystring>
   <!--- Returning our results to the caller --->

About This Tutorial
Author: Diego Benitez
Skill Level: Intermediate 
Platforms Tested: CFMX7
Total Views: 97,005
Submission Date: January 11, 2006
Last Update Date: June 05, 2009
All Tutorials By This Autor: 3
Discuss This Tutorial
  • Hey, this is a great way to do this! You really saved me a lot of trouble. Thanks!


Website Designed and Developed by Pablo Varando.