Dynamic Variable Naming and Reference (ColdFusion)

前端 未结 2 1298
长情又很酷
长情又很酷 2020-12-04 02:08

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

相关标签:
2条回答
  • 2020-12-04 02:39

    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>
    
    0 讨论(0)
  • 2020-12-04 02:50

    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>
    
    0 讨论(0)
提交回复
热议问题