如何在ColdFusion中的cfloop over查询中获取动态属性名称 [英] How to get a dynamic attribute name in cfloop over query in ColdFusion

查看:176
本文介绍了如何在ColdFusion中的cfloop over查询中获取动态属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在查询中的 cfloop 内。我想获得一个属性,但我不知道什么属性将在运行时。使用 #qryResult [MyAttr]#失败,并显示错误复杂对象类型无法转换为简单值。

I'm inside a cfloop over a query. I want to get an attribute, but I won't know what that attribute will be until runtime. Using #qryResult[MyAttr]# fails with the error "Complex object types cannot be converted to simple values." What is the syntax for doing this?

这是一个简化的例子:

<cfquery datasource="TestSource" name="qryResult">
    SELECT * FROM MyTable
</cfquery>

<cfloop query="qryResult">
    <cfset MyAttr="autoid" />
    <cfoutput>
        Test 1: #qryResult.autoid# <br/>  <!--- succeeds --->
        Test 2: #qryResult[MyAttr]# <br/> <!--- fails --->
    </cfoutput>
</cfloop>


推荐答案

<cfloop query="qryResult">
  <cfset MyAttr="autoid" />
  <cfoutput>
   Test 1: #qryResult.autoid# <br/>  <!--- succeeds --->
   Test 2: #qryResult[MyAttr][qryResult.CurrentRow]# <br/> <!--- succeeds --->
  </cfoutput>
</cfloop>

CurrentRow query.col )。它与索引< cfloop query =...> / < cfoutput query =...> ;

CurrentRow is implicit in the literal syntax (query.col). It is tied to the index of <cfloop query="...">/<cfoutput query="..."> (or 1 when used outside a loop).

在数组索引语法中显式地提及它是必要的(<$ c $因为 query [col] 单独返回列对象(这是复杂类型错误指的是)。

Mentioning it explicitly is necessary in the "array index" syntax (query[col][row]), because query[col] alone returns the column object (which is the "complex type" the error refers to).

副作用:您可以使用它来随机访问循环外的查询结果(即作为多维数组)。一旦知道您感兴趣的行数,就可以直接访问这些行。

Side effect: You can use this for random access to a query result outside of a loop (i.e. as a multi-dimensional array). Once you know the numbers of the rows that interest you, you can access the rows directly.

这篇关于如何在ColdFusion中的cfloop over查询中获取动态属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆