CFLib.org – Common Function Library Project

StructToQueryString(struct[, keyValueDelim][, queryStrDelim])

Last updated December 17, 2001

author

Erki Esken

Version: 1 | Requires: CF5 | Library: DataManipulationLib

Description:
Converts a structure to a URL query string. Query string delimiters can be changed: second function argument is the key/value delimiter (default is =) and third argument is the other delimiter (default is &).

Return Values:
Returns a string.

Example:

<cfscript>
   test = StructNew();
   test.foo = "bar";
   test.another = "foobar";
   normalQueryString = StructToQueryString(test);
   sesQueryString = StructToQueryString(test, "/", "/");
</cfscript>

<cfdump var="#normalQueryString#">
<P>
<cfdump var="#sesQueryString#">

Parameters:

Name Description Required
struct Structure of key/value pairs you want converted to URL parameters Yes
keyValueDelim Delimiter for the keys/values. Default is the equal sign (=). No
queryStrDelim Delimiter separating url parameters. Default is the ampersand (&). No

Full UDF Source:

/**
 * Converts a structure to a URL query string.
 * 
 * @param struct 	 Structure of key/value pairs you want converted to URL parameters 
 * @param keyValueDelim 	 Delimiter for the keys/values.  Default is the equal sign (=). 
 * @param queryStrDelim 	 Delimiter separating url parameters.  Default is the ampersand (&). 
 * @return Returns a string. 
 * @author Erki Esken (erki@dreamdrummer.com) 
 * @version 1, December 17, 2001 
 */
function StructToQueryString(struct) {
  var qstr = "";
  var delim1 = "=";
  var delim2 = "&";

  switch (ArrayLen(Arguments)) {
    case "3":
      delim2 = Arguments[3];
    case "2":
      delim1 = Arguments[2];
  }
	
  for (key in struct) {
    qstr = ListAppend(qstr, URLEncodedFormat(LCase(key)) & delim1 & URLEncodedFormat(struct[key]), delim2);
  }
  return qstr;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson