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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can you do a "changing variable" form request in Cold Fusion? 2

Status
Not open for further replies.

Ayac

Programmer
Nov 10, 2000
141
HU
Hi All!

It is probably an easy one for a CF programmer, but I am switching over from ASP and having a hard time with this.
I know this code is bad. It suppose to fill Query String variables into an array where the variable names are Data1, Data2, Data3... Data10 and list them, on screen. What do I need to put into the underlined area or is it even possible in ColdFusion to do it this way?

<CFSET DataT=ArrayNew(1)>
<CFLOOP index="i" from="1" to="10">
<CFSET Datas="Data" & i>
<CFIF IsDefined("Form." & Datas)>
<CFSET DataT=Form.____>
<CFELSE>
<CFSET DataT="">
</CFIF>
<cfoutput>
#DataT#
</cfoutput>
</CFLOOP>
 
i would do it like this..

<CFSET DataT=ArrayNew(1)>
<CFLOOP index="i" from="1" to="10">
<cfset varname = "FORM.data" & #i#>
<CFPARAM name="#varname#" default="">
<CFSET DataT=#Evaluate(varname)#>
<cfoutput>
#DataT#
</cfoutput>
</CFLOOP>

still not pretty but it works
 
It was very helpful! Thank you!!
 
A note: I'm gonna have to thank csteinhilber for again and again showing us the better method. You can (quoting) "treat the form scope as a structure, since in fact it is." And access it like this.

Code:
<CFSET DataT=ArrayNew(1)>
<CFLOOP index="i" from="1" to="10">
  <cfset varname = "data" & #i#>
  <CFPARAM name="form.#varname#" default="">
  <CFSET DataT[i]=#FORM[varname]#>
  <cfoutput>#DataT[i]#</cfoutput>
</CFLOOP>

This is trivial mostly but I've heard it best to stay away from evaluate when possible.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top