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 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:

<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

Shawn Porter Shawn Porter added
DeMoronize
3 hour(s) ago

Chris Carey Chris Carey added
readPropertiesFi...
1 day(s) ago

Randy Johnson Randy Johnson added
lastDayofWeek
3 day(s) ago

Frank Marion Frank Marion added
sitemapPing
7 day(s) ago

Top Rated

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Barney Boisvert indentXml
Rated 5.0, 3 time(s)

Nathan Dintenfass                                 queryColumnsToSt...
Rated 5.0, 3 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson