Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CFML - query row to structure

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.

like image 845
Daniel Avatar asked Jun 17 '11 17:06

Daniel


3 Answers

This is the Class from Ben's site without the comments

<--- --------------------------------------------------------------------------------------- ----

    Blog Entry:
    Ask Ben: Converting A Query To A Struct

    Author:
    Ben Nadel / Kinky Solutions

    Link:
    http://www.bennadel.com/index.cfm?event=blog.view&id=149

    Date Posted:
    Jul 19, 2006 at 7:32 AM

---- --------------------------------------------------------------------------------------- --->


<cffunction name="QueryToStruct" access="public" returntype="any" output="false"
    hint="Converts an entire query or the given record to a struct. This might return a structure (single record) or an array of structures.">
    <cfargument name="Query" type="query" required="true" />
    <cfargument name="Row" type="numeric" required="false" default="0" />

    <cfscript>
        var LOCAL = StructNew();
        if (ARGUMENTS.Row){
            LOCAL.FromIndex = ARGUMENTS.Row;
            LOCAL.ToIndex = ARGUMENTS.Row;
        } else {
            LOCAL.FromIndex = 1;
            LOCAL.ToIndex = ARGUMENTS.Query.RecordCount;
        }
        LOCAL.Columns = ListToArray( ARGUMENTS.Query.ColumnList );
        LOCAL.ColumnCount = ArrayLen( LOCAL.Columns );
        LOCAL.DataArray = ArrayNew( 1 );
        for (LOCAL.RowIndex = LOCAL.FromIndex ; LOCAL.RowIndex LTE LOCAL.ToIndex ; LOCAL.RowIndex = (LOCAL.RowIndex + 1)){
            ArrayAppend( LOCAL.DataArray, StructNew() );
            LOCAL.DataArrayIndex = ArrayLen( LOCAL.DataArray );
            for (LOCAL.ColumnIndex = 1 ; LOCAL.ColumnIndex LTE LOCAL.ColumnCount ; LOCAL.ColumnIndex = (LOCAL.ColumnIndex + 1)){
                LOCAL.ColumnName = LOCAL.Columns[ LOCAL.ColumnIndex ];
                LOCAL.DataArray[ LOCAL.DataArrayIndex ][ LOCAL.ColumnName ] = ARGUMENTS.Query[ LOCAL.ColumnName ][ LOCAL.RowIndex ];
            }
        }
        if (ARGUMENTS.Row){
            return( LOCAL.DataArray[ 1 ] );
        } else {
            return( LOCAL.DataArray );
        }
    </cfscript>
</cffunction>

usage...

<!--- Convert the entire query to an array of structures. --->
<cfset arrGirls = QueryToStruct( qGirls ) />

<!--- Convert the second record to a structure. --->
<cfset objGirl = QueryToStruct( qGirls, 2 ) />
like image 162
Daniel Avatar answered Nov 11 '22 09:11

Daniel


Adobe ColdFusion 11 introduced QueryGetRow which converts a row from a query into a struct.

like image 41
Code-Apprentice Avatar answered Nov 11 '22 10:11

Code-Apprentice


Ben Nadel has posted a blog post about this where he gives an example UDF that converts a query to a struct, and it accepts an optional row argument that allows you to turn an single row in that query to a struct. Take a look here.

like image 3
Sean Coyne Avatar answered Nov 11 '22 10:11

Sean Coyne