CFLib.org – Common Function Library Project

QueryToStructOfArrays(query)

Last updated February 23, 2002

Version: 1 | Requires: ColdFusion 5 | Library: DataManipulationLib

 
Rated 2 time(s). Average Rating: 4.0

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:

view plain print about
<CFSET Query = QueryNew("id,name,age")>
<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:

view plain print about
<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>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 22 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson