• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

CFML help.. writing loop with variables within variables

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.
 
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.
 
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.
 
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
 
Back
Top