CFML - 查询行结构

时间:2011-06-17 17:28:06

标签: coldfusion cfloop

我想通过函数处理查询中的一行,我将该行作为结构传递。

...理想地

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

我也可以这样设置......

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

但我不想。我一直无法找到提取行的简单方法,但我想我会问。

5 个答案:

答案 0 :(得分:14)

找到了一个更优雅的解决方案,用于单行

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

答案 1 :(得分:7)

Adob​​e ColdFusion现在有一个名为QueryGetRow的函数,它将查询中的行转换为结构。

<强>附录

此功能在ColdFusion 9及更早版本中不可用。

答案 2 :(得分:3)

Ben Nadel发布了一篇关于此的博客文章,他给出了一个将查询转换为结构的UDF示例,它接受一个可选的行参数,允许您将该查询中的单行转换为结构。看看here

答案 3 :(得分:3)

这是本网站上没有评论的班级

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

    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>

...使用

<!--- 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 ) />

答案 4 :(得分:0)

另一种解决方案是:

function QueryToStruct(query){
    var cols    = ListToArray(query.columnList);
    var salida  = query.map(function(v=0,i,a){
                       return {'#cols[1]#':v};
                  });  
    return ValueArray(salida,'#cols[1]#');
}