user460114
user460114

Reputation: 1848

Dynamic variable containing numbers

I've tried everything I've found that might be applicable to this, without success to date.

I have variables that look like

x000Foo

I'm trying to create one of these dynamically in form scope from query results and have tried the following, and a few others, without success:

<cfloop query="qFormFields">

    <cfset "form.x000#fieldname#" = 0>
    You have attempted to dereference a scalar variable of type class coldfusion.sql.QueryColumn as a structure with members

    <cfset "form.x[000]#fieldname#" = 0>
    The value x000AA_report cannot be converted to a number. 

    <cfset form["x000#fieldname#"] = 0>
    The value x000AA_report cannot be converted to a number. 

</cfloop

I know it's related to the zeros, but I'm not sure how to get around it without resorting to renaming these variables throughout the application.

I'm on ColdFusion2016

Upvotes: 0

Views: 316

Answers (2)

Chester
Chester

Reputation: 1083

I'm not sure if this is what you're trying to do: but here's how you can do dynamic variables:

<cfset fieldname = "foo">
<cfset form["x000" & fieldname] = 0>
<cfdump var="#form#">
<!--- variable form.x000Foo = 0 --->

Runnable example on TryCF.com

Upvotes: 3

Kishore Vaishnav
Kishore Vaishnav

Reputation: 510

you can try evaluate function read more

<cfloop query="qFormFields">
      <cfset fieldvalue = Evaluate("form.x000#fieldname#")>
      <cfdump var="#fieldvalue#">
</cfloop>

let me know if it is working or not.

Upvotes: 0

Related Questions