CFML - query row to structure

Daniel picture Daniel · Jun 17, 2011 · Viewed 14k times · Source

I would like to handle a row from a query by a function, where I pass the row as a structure.

ideally...

<cfloop query="myquery">
 #myfunction(#row#)#
</cfloop>

I could set it up like this too...

<cfloop query="myquery">
 #myfunction(#col1#,#col2#,#col3#,#col4#)#
</cfloop>

but I don't want to. I haven't been able to finda simple way of extracting a row, But I thought I'd ask.

Answer

Daniel picture Daniel · Jun 24, 2011

found a little more elegant looking solution, for single row

<cfscript>
    function GetQueryRow(query, rowNumber) {
        var i = 0;
        var rowData = StructNew();
        var cols    = ListToArray(query.columnList);
        for (i = 1; i lte ArrayLen(cols); i = i + 1) {
            rowData[cols[i]] = query[cols[i]][rowNumber];
        }
        return rowData;
    }
</cfscript>