【问题标题】:How to dynamically construct query output variables in Coldfusion?如何在 Coldfusion 中动态构造查询输出变量?
【发布时间】:2013-02-08 07:06:27
【问题描述】:

我正在尝试遍历包含我需要显示的列名的数组。用户可以定义自己的列,因此这将是一个动态列名列表

例如,列名可能是:

["style", "color", "size"]

这些是我需要从名为results 的查询中输出的列名。

我正在这样做:

<cfset variables.styleText = "">
<cfloop array="#DeserializeJSON(variables.raw.field_names)#" index="x">
   <cfset variables.styleText = variables.styleText & "#results." & x &"# ">
</cfloop>
<cfoutout>variables.styleText</cfoutput>

但这给了我一个错误,因为我不能用 quot aka 结束变量名

 Diagnose: A CFML variable name cannot end with a &quot;.&quot; character. 
 The variable results. ends with a &quot;.&quot; character.  
 You must supply an additional structure key or delete the &quot;.&quot; character.

问题:
谁能给我一个提示,在这种情况下,我需要如何修改它以输出我的 results 查询中的值 #results.style# #results.color# #results.size#?

谢谢!

【问题讨论】:

    标签: variables dynamic coldfusion output


    【解决方案1】:

    而不是

    <cfloop array="#DeserializeJSON(variables.raw.field_names)#" index="x">
     <cfset variables.styleText = variables.styleText & "#results." & x &"# ">
    </cfloop>
    

    你应该可以的

    <cfloop array="#DeserializeJSON(variables.raw.field_names)#" index="x">
     <cfset variables.styleText = variables.styleText & results[x][results.currentrow]>
    </cfloop>
    

    或者,如果您只是使用 CF9 或更高版本

    <cfset variables.styleText&=results[x][results.currentrow]>
    

    【讨论】:

    • 好的。试。谢谢。不过使用CF8,所以我会接受你的第一个建议
    • 酷。作品。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多