动态变量命名和引用

动态变量命名和引用

本文介绍了动态变量命名和引用(ColdFusion)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快乐星期五全部,

试图破解这个为期几天。我想要做的是动态创建一个变量使用一个固定字符串和一个变量(数字)的组合。

Been trying to crack this for a couple days. What I am looking to do is dynamically create a variable using a combination of a fixed string and a variable (number).

我查询一个数据库,得到一个列表代理,然后我想为每个代理创建单独的变量来保存不同的统计数据。我正在寻找在循环(即,创建的票)当前的统计,然后添加每个代理ID到结束。

I am querying a DB to get a list of agents and then I want to create individual variables for each agent to hold different stats. I am looking to take the current stat in the loop (i.e., tickets created) and then add each agents ID to the end.

我调用这个第一个统计 GC Get Created ,因此具有当前统计和代理ID的示例变量将类似于 GC1

I am calling this first stat GC for Get Created so an example variable with the current stat and agent ID would be something like GC1.

我有Googled并且阅读了许多不同的提示,指南等,但大多数都已过时(2002年!)似乎是基于查询和数组。我正在寻找更简单的东西(或者我相信)。

I have Googled and read many, many different tips, guides, etc. but most of them are either outdated (2002!) or seem to be based off of queries and arrays. I'm looking for something much simpler (or so I believe).

我知道我应该能够使用沿着 > AID = Agent ID ):

I know I should be able to dynamically create the variable using something along the lines of (AID = Agent ID):

<cfset "GC#AID#" = getCreated.RecordCount>

现在,我没有错误,所以我假设变量正在创建正确,输出变量是我真正陷入问题的地方(或者我想)。

Now, I get no errors so I am assuming the variable is being created correctly however when I go to output the variable is where I am really coming into an issue (or so I think).

无论我尝试多少种不同的方式,输出总是 GC1 。而我希望变量的显示,变量名称。

No matter how many different ways I try this the output is always GC1. Instead I want the value of the variable displayed, not the variable name.

我知道我不想使用evaluate和我已经看到了许多其他网站上的例子和SO关于使用struct符号,但是这些总是与一个查询结合。

I know I don't want to use evaluate and I have seen numerous examples on other sites and on SO about using struct notation however these have always been in conjunction with a query.

我觉得我在这里必须缺少一些非常简单的东西。关于我如何能够输出动态创建的变量在查询上下文之外的的任何想法?

I feel like I must be missing something very simple here. Any thoughts on how I might be able to output the value of a dynamically created variable outside the context of a query?

请让我知道任何澄清是否有帮助。

Please let me know if any clarification would help.

我使用ColdFusion 10,如果相关。

I am using ColdFusion 10 if that is relevant.

提前感谢!

EDIT 1

EDIT 1

这是我目前输出代码的名称:

This is what I currently have for the Output Code:

<cfset test = "GC#variables.AID#">
Test: <cfoutput>#test#</cfoutput>

我意识到这可能是不可能的。

I realize this is probably off the mark.

推荐答案

一个选项:

设置动态变量名称:

<cfset variables["GC" & AID] = "Testing" />

输出动态变量名称的值:

Output the value of the dynamic variable name:

<cfoutput>#variables["GC" & AID]#</cfoutput>

另一个选项:

变量名称:

<cfset variables["GC#AID#"] = "Testing" />

输出动态变量名称的值:

Output the value of the dynamic variable name:

<cfoutput>#variables["GC#AID#"]#</cfoutput>

这篇关于动态变量命名和引用(ColdFusion)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 12:34