动态变量命名和引用 (ColdFusion) [英] Dynamic Variable Naming and Reference (ColdFusion)

查看:20
本文介绍了动态变量命名和引用 (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.

我正在为 Get Created 调用第一个 stat GC,因此具有当前 stat 和代理 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.

我在谷歌上搜索并阅读了许多不同的提示、指南等,但其中大部分要么已经过时(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.

我知道我不想使用评估,并且我在其他网站和 SO 上看到了许多关于使用结构符号的示例,但是这些总是与查询结合使用.

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?

抱歉冗长的解释,只是想确保我说得清楚.

Sorry for the long winded explanation, just want to make sure I am being clear.

如果有任何澄清会有所帮助,请告诉我.

Please let me know if any clarification would help.

如果相关,我正在使用 ColdFusion 10.

I am using ColdFusion 10 if that is relevant.

提前致谢!

编辑 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" />

输出动态变量名的值:

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

另一种选择:

设置动态变量名:

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

输出动态变量名的值:

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

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

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