CFLib.org – Common Function Library Project

QueryRowToStruct(query [, row])

Last updated December 11, 2001

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

 
Rated 10 time(s). Average Rating: 4.7

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:

view plain print about
<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:

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

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
3 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

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

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson