Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Variable Naming and Reference (ColdFusion)

Happy Friday All,

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

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.

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.

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

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

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.

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.

I am using ColdFusion 10 if that is relevant.

Thanks in advance!

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.

like image 589
Caleb Palmquist Avatar asked Aug 23 '13 19:08

Caleb Palmquist


2 Answers

One Option:

Setting a dynamic variable name:

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

Output the value of the dynamic variable name:

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

Another Option:

Setting a dynamic variable name:

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

Output the value of the dynamic variable name:

<cfoutput>#variables["GC#AID#"]#</cfoutput>
like image 89
Jason Dean Avatar answered Oct 20 '22 15:10

Jason Dean


All variable scope are structs. So loop through that scope as a struct. As your code is written, it's in the variables scope. I'd put it in it's own struct, but here's a hacked up version of what you're trying to do:

<cfloop collection="#variables#" item="k">

    <cfif left(k,2) eq "GC">
        <cfoutput>#k# : #variables[k]#</cfoutput><br/>
    </cfif>

</cfloop>
like image 21
Billy Cravens Avatar answered Oct 20 '22 16:10

Billy Cravens