ColdFusion UDF Library - queryString

December 16th, 2008
«« Back to ColdFusion Library

Category:
   STRINGS

Description:
   queryString will analyze a string of name value pairs. It will change the value of any match in the new string, and will add any unfound name matches.

Parameters:
  1. str (String, required)
           New name value pairs to replace or add to the query string.

  2. query (String, Default: cgi.query_string)
           The name value pair string on which to perform the operation.

  3. redirect (Variant, Default: urlEncodedFormat(query))
           If true, will add redirect=urlEncodedFormat(query) to the returned string. If false, will add nothing to the returned string. If anything else, will set the redirect value in the returned string to that value.

  4. nvSep (String, Default: =)
           The delimiter between a name and value. Example, could be set to : for a style sheet.

  5. sep (String, Default: &)
           The delimiter between a name and value pair. Example, could be set to ; for a style sheet.


Returns:
       A new name value pair string with provided values set and/or added.

Requires:
       listFindNV(), listDeleteNV()

Sample Use:
        queryString("first_name=Shawn&age=29","first_name=Sean&last_name=Bailly");
Returns: first_name=Shawn&last_name=Bailly&age=29

queryString("color:white","background-color:black;color:blue",false,":",";");
Returns: background-color:black;color:white

Copy to Clipboard

/*#depends: listFindNV,listDeleteNV#*/
function queryString(str){
	var key = server.deskey;
	var query = cgi.query_string;
	var redirect = "";
	var addRedirect = true;
	var nvSep = "=";
	var sep = "&";
	var loop = 0;
	var name = "";
	var value = "";
	var pos = 0;
	var tmp = "";
	
	//get querystring
	if (arrayLen(arguments) gte 2)query = arguments[2];
	
	//control redirect
	if (arrayLen(arguments) gte 3){
		if (isBoolean(arguments[3]))addRedirect = arguments[3];
		else redirect = urlEncodedFormat(arguments[3]);
	}
	
	//name value pair "="
	if (arrayLen(arguments) gte 4)nvSep = arguments[4];
	
	//set = "&"
	if (arrayLen(arguments) gte 5)sep = arguments[5];
		
	tmp = server.listDeleteNV(query,"redirect");
	
	if (len(tmp))redirect = encrypt(tmp,key,"des","hex");
	
	tmp = listLen(str,sep);
	
	for (loop = 1; loop lte tmp; loop = IncrementValue(loop)){
		name = listFirst(listGetAt(str,loop,sep),nvSep);
		if (listLen(listGetAt(str,loop,sep),nvSep) gte 2)value = listLast(listGetAt(str,loop,sep),nvSep);
		else value = "";
		pos = server.listFindNV(query,name);
		if (pos)query = listSetAt(query,pos,"#name##nvSep##value#",sep);
		else query = listAppend(query,"#name##nvSep##value#",sep);
	};
	if (addRedirect){
		pos = server.listFindNV(query,"redirect");
		if (pos)query = listSetAt(query,pos,"redirect#nvSep##redirect#",sep);
		else query = listAppend(query,"redirect#nvSep##redirect#",sep);
	}
	return query;
}
// http://www.programmers.org/index.php/coldfusion-udf-library/?ckey=73&function=queryString
  1. No comments yet.
  1. No trackbacks yet.