QueryToStructOfArrays(query)
Last updated February 23, 2002
Version: 1 | Requires: ColdFusion 5 | Library: DataManipulationLib
Description:
Changes a query into a struct of arrays, where the keys of the struct are the column names in the query. Each struct key holds an array of the values from the query. See also QueryToArrayOfStructs.
Return Values:
Returns a structure.
Example:
<CFLOOP INDEX="X" FROM=1 TO=3>
<CFSET QueryAddRow(Query,1)>
<CFSET QuerySetCell(Query,"ID",X,X)>
<CFSET QuerySetCell(Query,"Name","Name #X#",X)>
<CFSET QuerySetCell(Query,"Age",X+15,X)>
</CFLOOP>
<CFSET Struct = QueryToStructOfArrays(Query)>
<CFDUMP VAR="#Struct#">
Parameters:
| Name | Description | Required |
|---|---|---|
| query | The query to be transformed. | Yes |
Full UDF Source:
<cfscript>
/**
* Changes a query into a struct of arrays.
*
* @param query The query to be transformed.
* @return Returns a structure.
* @author Nathan Dintenfass (nathan@changemedia.com)
* @version 1, February 23, 2002
*/
function queryToStructOfArrays(q){
//a variable to hold the struct
var st = structNew();
//two variable for iterating
var ii = 1;
var cc = 1;
//grab the columns into an array for easy looping
var cols = listToArray(q.columnList);
//iterate over the columns of the query and create the arrays of values
for(ii = 1; ii lte arrayLen(cols); ii = ii + 1){
//make the array with the col name as the key in the root struct
st[cols[ii]] = arrayNew(1);
//now loop for the recordcount of the query and insert the values
for(cc = 1; cc lte q.recordcount; cc = cc + 1)
arrayAppend(st[cols[ii]],q[cols[ii]][cc]);
}
//return the struct
return st;
}
</cfscript>
Search CFLib.org
Latest Additions
Dave Anderson added
iniToStruct
20 day(s) ago
Dave Anderson added
deDupeArray
20 day(s) ago
Richard added
dice
22 day(s) ago
Isaac Dealey added
getRelative
a while ago
Top Rated
backupDatabase
Rated 5.0, 22 time(s)
indentXml
Rated 5.0, 10 time(s)
generateSsccAsn
Rated 5.0, 4 time(s)
highlightAndCrop
Rated 5.0, 4 time(s)