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;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson