CFML help.. writing loop with variables within variables

TechBoyJK

Lifer
Oct 17, 2002
16,699
60
91
I'm trying run the loop that is attached, and it's breaking at this line

<CFIF isDefined("FORM.select_#index#") and (FORM.select_#index# NEQ "")>

and it isn't rendering the #index#

Any clues on teh best way to make this work? the logic of what needs to work is in the code, but I need to correct the syntax.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Take a look at the Evaluate function. It's been a long time since I've used CF, but I think that's what you want.
 

drebo

Diamond Member
Feb 24, 2006
7,034
1
81
Enclose the variable name in quotations, a la:

<cfif "FORM.select_#index#" neq "">

Alternatively, you can address the FORM variable as an array. The following would be equivalent:

<cfif FORM['select_#index#'] neq "">

As a third option, which is useful when you don't know exactly how many options you'll have, loop through FORM.FIELDNAMES and test whether the left n characters are what you'd expect them to be:

<cfloop list="#FORM.FIELDNAMES#" index="i" delimiters=",">
<cfif Left(i, 7) eq "select_">
<cfset index = GetToken(i,2,"_")>
<!--- process your info here, with i being your actual form field name and index is now the index number of that field --->
</cfif>
</cfloop>

I use this method most often, I think, as when I end up having dynamic form field names, I typically don't know exactly how many I'm going to have. In addition, they may not always be numbered continuously from start to finish. If you start skipping, your script becomes either very inefficient or will error out.

Edit: Also, make sure you use <cfqueryparam> whenever you are sticking dynamic content into your queries (or use stored procedures). There was a SQL injection exploit for ColdFusion a couple months ago that was very bad. I'd hate to see it come up on anyone.
 

TechBoyJK

Lifer
Oct 17, 2002
16,699
60
91
Originally posted by: drebo
Enclose the variable name in quotations, a la:

<cfif "FORM.select_#index#" neq "">

Alternatively, you can address the FORM variable as an array. The following would be equivalent:

<cfif FORM['select_#index#'] neq "">

As a third option, which is useful when you don't know exactly how many options you'll have, loop through FORM.FIELDNAMES and test whether the left n characters are what you'd expect them to be:

<cfloop list="#FORM.FIELDNAMES#" index="i" delimiters=",">
<cfif Left(i, 7) eq "select_">
<cfset index = GetToken(i,2,"_")>
<!--- process your info here, with i being your actual form field name and index is now the index number of that field --->
</cfif>
</cfloop>

I use this method most often, I think, as when I end up having dynamic form field names, I typically don't know exactly how many I'm going to have. In addition, they may not always be numbered continuously from start to finish. If you start skipping, your script becomes either very inefficient or will error out.

Edit: Also, make sure you use <cfqueryparam> whenever you are sticking dynamic content into your queries (or use stored procedures). There was a SQL injection exploit for ColdFusion a couple months ago that was very bad. I'd hate to see it come up on anyone.


awesome. i didnt know you could loop through the fieldnames like that. thankx