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

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

问题描述

我在一个 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="..."> 的索引相关联(或 1 时在循环外使用).

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

在数组索引"语法(query[col][row])中明确提及它是必要的,因为 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.

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

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