Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concatenating variable names 1

Status
Not open for further replies.

spook007

Programmer
Joined
May 22, 2002
Messages
259
Location
US
I would like to collect data from a form, and store the data in an array. The field names in the form have been named.

totalQuestions
1Question
2Question
3Question

In order to put the values from these fields into the array I would like to run them through a loop.
(the number of questions will vary, totalQuestions is a hidden field that will store the number of questions.)

<cfloop index=&quot;i&quot; from=&quot;1&quot; to=&quot;#form.totalQuestions#&quot;>
<cfset aAnswers=evaluate(#form.#&i&#Question#)>
</cfloop>

I also tried switching the field names to:

totalQuestions
Question1
Question2
Question3

<cfloop index=&quot;i&quot; from=&quot;1&quot; to=&quot;#form.totalQuestions#&quot;>
<cfset aAnswers=evaluate(&quot;#form.Question#i&quot;)>
</cfloop>

Does anyone have any suggestions as far as how I can accomplish this? Thanks
 
The Evaluate() function takes a string and tries to execute it as a ColdFusion statement. So the string you pass it needs to be formatted properly.

Let's say you had a FORM field called
Code:
Question1
that had a value, when submitted, of &quot;
Code:
Hello
&quot;.

The action page would receive the post and set up
Code:
FORM.Question1
to equal &quot;
Code:
Hello
&quot;. No big secret there.

But now let's look at your Evaluate line:
Code:
<cfset aAnswers=evaluate(&quot;#form.Question#i&quot;)>

Look at it how ColdFusion would see it as it's evaluating it.
Code:
#FORM.Question#
would probably not be declared... so you're trying to Evaluate the string &quot;i&quot;. So aAnswers would equal &quot;i&quot;

So solve the immediate problem, switch your pound signs around so you're replacing the proper portion of the string with the dynamic value:
Code:
<cfset aAnswers=evaluate(&quot;form.Question#i#&quot;)>
this way, if
Code:
i
is equal to &quot;1&quot;, then the string that is being evaluated is now &quot;form.question1&quot;... which is what you want.

However... you'll find that Evaluate is actually very slow. You really want to avoid using it when you can. Happily, the FORM scope is actually a standard ColdFusion structure... so you can access keys within that structure dynamically without using Evaluate.

Try:
Code:
<cfloop index=&quot;i&quot; from=&quot;1&quot; to=&quot;#form.totalQuestions#&quot;>
  <cfset aAnswers = form[&quot;Question&quot; & i]>
</cfloop>

I think you'll be much happier that way, than if you were doing a lot of Evaluates.


-Carl
 
Ummm... hold on... the other problem is that, of course,
Code:
aAnswers
will always be the last value... since you overwrite the previous value(s) with each step of the loop.

So you'll, of course, what to set up the array that you wanted in the first place:

Code:
<CFSET aryQuestions = ArrayNew(1)>
<CFLOOP index=&quot;i&quot; from=&quot;1&quot; to=&quot;#form.totalQuestions#&quot;>
  <cfset aryQuestions[i] = form[&quot;Question&quot; & i]>
</CFLOOP>




-Carl
 
Carl;

You gave a golden explanation and your solution worked like a charm. I certainly appreciate the time you took to answer my question. Thanks!!
 
Carl

I want to thank you for the very descriptive answer. I was working on a totally different script for my job but the &quot;form[....]&quot; line with the loop solved a problem I was working on for 2 hours.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top