Skip to main content
Participant
July 8, 2006
Answered

dynamic structure names in cfloop

  • July 8, 2006
  • 4 replies
  • 693 views
I have designed a web application that allows users to enter a number of records on one form – for example computer skills. The user can add up to 20 different computer skills.

On another form, the user’s recorded computer skills are shown as a number of dynamic checkboxes. One computer skill means the user sees one checkbox. Twenty computer skills means the user sees 20 checkboxes. I have made a query loop to output these computer skills and checkboxes. I have also made structure variables so that the selection of the specific computer skill checkbox is retained for later use in the web application. If the user checks any of the checkboxes, on submission of the form, the structure value is updated from no to yes (and vice versa). This part is working, and I am happy with it.

The problem I have is that I am unable to find a way to individually indicate that the specific checkbox value is checked or not. If the user selects the checkbox, submits the form and returns to the form, the checkbox should still show as ticked as the structure value of that checkbox is “yes” (as in checked=”yes”). As I am using structures, adding #currentRow# to the structure name inside the loop does not work, as CF views this as a different structure name that is not recognised. For example:

<cfloop query=”rsReturnComputerSkills

<cfinput type=”checkbox” name=”computerSkill#currentRow#” checked=”#structure.computerSkill##currentRow#”

</cfloop>

Essentially, I cannot place the dynamic structure value into the checkbox input field.

I thought that using an array may overcome this, but alas I encounter the same problem.

Is there a way to append #currentRow# to the structure name or is there another way to achieve the same that I have not thought of?
This topic has been closed for replies.
Correct answer FattMatt
The answer is evaluate()

<cfset loopCheckValue = " ">

<cfloop query=”rsReturnComputerSkills>
<cfset loopCheckValue = evaluate("structure.computerSkill" & "#currentRow#")>
<input type=”checkbox” #loopCheckValue# name=”computerSkill#currentRow#”>
</cfloop>

4 replies

Inspiring
July 10, 2006
There has been some evidence that this may not be as true in the current
MX days then it was in CF 5 and before.

But I find the bracket[] notion cleaner and one can certainly do more
with it then one can easily do with evaluate() function(s).

Kevin Schmidt wrote:
> Ian is right. You want to avoid using evaluate() if possible. It's just not efficient.
Participating Frequently
July 10, 2006
Interesting. Do you have the info on that Ian. As far as I know, at least in the latest Advanced CF Course, Adobe/MM was still saying that evaluate() brought with it a performance degradation.
Participating Frequently
July 10, 2006
Ian is right. You want to avoid using evaluate() if possible. It's just not efficient.
Inspiring
July 10, 2006
A better answer is the [] notation for arrays and structures.

<cfset loopCheckValue = structure['computerSkill' & currentRow]>

FattMatt wrote:
> The answer is evaluate()
>
> <cfset loopCheckValue = " ">
>
> <cfloop query=?rsReturnComputerSkills>
> <cfset loopCheckValue = evaluate("structure.computerSkill" & "#currentRow#")>
> <input type=?checkbox? #loopCheckValue# name=?computerSkill#currentRow#?>
> </cfloop>
>
FattMattAuthorCorrect answer
Participant
July 8, 2006
The answer is evaluate()

<cfset loopCheckValue = " ">

<cfloop query=”rsReturnComputerSkills>
<cfset loopCheckValue = evaluate("structure.computerSkill" & "#currentRow#")>
<input type=”checkbox” #loopCheckValue# name=”computerSkill#currentRow#”>
</cfloop>