CFLib.org – Common Function Library Project

QueryRowToStruct(query [, row])

Last updated December 11, 2001
Download UDF

author

Nathan Dintenfass                                 Nathan Dintenfass

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

 
Rated 4 time(s). Average Rating: 4.3

Description:
Convenience function for turning a given row of query into a structure. By default, it uses the first row.

Return Values:
Returns a structure.

Example:

<cfscript>
    q = querynew("id,name");
    queryAddRow(q);
    querySetCell(q,"id",1);
    querySetCell(q,"name","Nathan Dintenfass");
    queryAddRow(q);
    querySetCell(q,"id",2);
    querySetCell(q,"name","Ben Archibald");    
    queryAddRow(q);
    querySetCell(q,"id",3);
    querySetCell(q,"name","Raymond Camden");        
</cfscript>

<cfdump var="#queryRowToStruct(q)#">
<cfdump var="#queryRowToStruct(q,2)#">

Parameters:

Name Description Required
query The query to work with. Yes
row Row number to check. Defaults to row 1. No

Full UDF Source:

<cfscript>
/**
* Makes a row of a query into a structure.
*
* @param query      The query to work with.
* @param row      Row number to check. Defaults to row 1.
* @return Returns a structure.
* @author Nathan Dintenfass (nathan@changemedia.com)
* @version 1, December 11, 2001
*/

function queryRowToStruct(query){
    //by default, do this to the first row of the query
    var row = 1;
    //a var for looping
    var ii = 1;
    //the cols to loop over
    var cols = listToArray(query.columnList);
    //the struct to return
    var stReturn = structnew();
    //if there is a second argument, use that for the row number
    if(arrayLen(arguments) GT 1)
        row = arguments[2];
    //loop over the cols and build the struct from the query row    
    for(ii = 1; ii lte arraylen(cols); ii = ii + 1){
        stReturn[cols[ii]] = query[cols[ii]][row];
    }        
    //return the struct
    return stReturn;
}
</cfscript>

Search CFLib.org


Latest Additions

Raymond Camden Raymond Camden added
romanToDecimal
3 day(s) ago

Joe Rinehart Joe Rinehart added
directoryCopy
4 day(s) ago

Marcos Placona Marcos Placona added
arrayGroupsOf
4 day(s) ago

Mosh Teitelbaum Mosh Teitelbaum added
formatListAsSeri...
25 day(s) ago

Top Rated

Nick Giovanni                                     UniqueValueList
Rated 5.0, 5 time(s)

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Bobby Hartsfield                                  randStr
Rated 5.0, 3 time(s)

Rob Brooks-Bilson                                 FolderSize
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson