tags:

views:

111

answers:

1

I have a table in a form with a loop around it that sets an index on all the name fields (form variables) that changes based on user input (user dictates how many tables get built). Once submitted, I take these variables and create a struct out of it. I know that a query object is already a struct, but I have to have each tables names unique, then put them into a struct which I can then rename to insert into the DB. My problem is how do I write this correctly and efficiently? I need to have a variable with a nested variable that is my index to make the name unique for each iteration through the loop. I have tried many combinations of pound signs and quotes and can't get it. If there is a better way to do this I am up for that too! dot notation

  • cfset myStruct#i#=StructNew()>
  • cfset myStruct#i#.ID#i#="#form.myVarA#i##"
  • cfset myStruct#i#.s1#i#="#form.myVarB#i##"
  • cfset myStruct#i#.s2#i#="#form.myVarC#i##"

associated array notation

  • cfset myStruct#i#=StructNew()>
  • cfset myStruct#i#[ID#i#]="#form.myVarA#i##"
  • cfset myStruct#i#[s1#i#]="#form.myVarB#i##"
  • cfset myStruct#i#[s2#i#]="#form.myVarC#i##"

Any help is greatly appreciated.

+6  A: 

This is the best reference you'll ever need to understand variables in CFML:

http://www.depressedpress.com/Content/Development/ColdFusion/Guides/Variables/Index.cfm

To answer your question, try this:

<cfset myStruct["#i#"] = structNew() />
<cfset myStruct["#i#"]["ID#i#"] = form["myVarA#i#"] />
<cfset myStruct["#i#"]["s1#i#"] = form["myVarB#i#"] />

This should give you:

myStruct.1.id1 = form.myvarA1 myStruct.1.s11 = form.myvarB1

Antony
Huge thanks - works like a charm and a great reference as well!
JS
No need for the "s and #s in the first part. <cfset myStruct[i] ...>
Patrick McElhaney
Al Everett
@Patrick - true, but I didn't want to change approach half way.
Antony