Coldfusion - 循环访问数据库查询结果时的变量字段名称 [英] Coldfusion - variable field name when looping through database query results

查看:140
本文介绍了Coldfusion - 循环访问数据库查询结果时的变量字段名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表格中有一组列名称,例如foo1,foo2,foo3,foo4。我想通过循环动态引用这些列名:

I have a set of column names in a table - e.g. foo1, foo2, foo3, foo4. I want to refer to these column names dynamically through a loop:

<cfloop index="i" from="1" to="4">
  <cfset foo = Evaluate("query.foo" & i)>
</cfloop>

上面的方法不工作 - ColdFusion抛出变量未定义错误, > query.foo1 是对查询结果的有效引用。

The above doesn't work - ColdFusion throws a "variable not defined" error, even though query.foo1 is a valid reference to the query results. How else can I do this?

推荐答案

不要使用 Evaluate()对于这样的事情!这很慢,应避免

Don't use Evaluate() for things like that! It's slow and should be avoided.

<cfloop index="i" from="1" to="4">
  <cfset foo = query["foo" & i][query.CurrentRow]>
</cfloop>

或者,如果您喜欢:

<cfloop index="i" from="1" to="4">
  <cfset foo = query["foo#i#"][query.CurrentRow]>
</cfloop>

Evaluate()码。

编辑:

当使用尖括号-syntax访问Query对象时,必须附加(从1开始)行号索引( query [foo#i#] [RowNum] code>)。当使用传统的dot-syntax( query.foo1 )时,当前行是隐式的。

When accessing Query objects with the "angle bracket"-syntax, you must append the (1-based) row number index (query["foo#i#"][RowNum]). When using the traditional "dot"-syntax (query.foo1), the current row is implicit.

要显式访问当前行,请使用 QueryObject.CurrentRow 属性。但它可以是任何正整数,最多 QueryObject.RecordCount 。除了 CurrentRow 之外,建议范围检查。

To access the current row explicitly, use the QueryObject.CurrentRow property. But it could be any positive integer up to QueryObject.RecordCount. A range check is advised for anything other than CurrentRow.

这将打开一个有趣的字段:您可以开始使用查询对象具有随机访问。以前(在CFMX之前)你可以做的是从头到尾迭代它们,拉出你寻找的东西。现在它就像一个嵌套的结构/数组数据结构,你可以使用不同的方式。

This opens an interesting field: You can start to use query objects with "random access". Previously (before CFMX) all you could do was iterate them from start to end, pulling out the things that you look for. Now it's like a nested struct/array data structure that you can use in different ways.

这篇关于Coldfusion - 循环访问数据库查询结果时的变量字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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