QueryStringSet

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"

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

Returns
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)

Example
<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 --->
<cfoutput>
    Source Query String : #oldQueryString#<br>
    Formated Query String : #newQueryString#
</cfoutput>

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>
      </cfloop>
      <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 --->
            </cfif>
         </cfloop>
      <cfelse>
         <cfset arguments.querystring = listappend(arguments.querystring,listgetat(arguments.variable,vCount) &
'=' & listgetat(arguments.value,vCount),'&')>
         <!--- If not variable found in querystring lets add it --->
      </cfif>
   </cfloop>
   <cfreturn arguments.querystring>
   <!--- Returning our results to the caller --->
</cffunction>



All ColdFusion Tutorials By Author: Diego Benitez
  • Format your date in spanish.
    This is a custom tag to translate the regular english output of a date into spanish like (20 de Enero de 2004) and as you want it to be.
    Author: Diego Benitez
    Views: 9,570
    Posted Date: Thursday, January 29, 2004
  • how to format a date/time using bind
    here is a long way but the only one i found to format a date/time using the <cfformgroup type="repeater" query="queryname">
    Author: Diego Benitez
    Views: 7,638
    Posted Date: Thursday, March 10, 2005
  • QueryStringSet
    Replaces or Adds a variable with its value on a cgi.Query_String alike. It supports multiple values at the same time separated by commas
    Author: Diego Benitez
    Views: 5,827
    Posted Date: Wednesday, January 11, 2006