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

Coldfusion loop

Status
Not open for further replies.

habesha

Programmer
Oct 30, 2006
68
US
I need help in correcting the following code.


To be more specific, i have created the form page with simple html and added "field1#CurrentRow#" to the name

<input type=&quot;Text&quot; name=&quot;field1#CurrentRow#&quot; size=&quot;8&quot; maxlength=&quot;20&quot;>

and the action page looks the following. But it doesnt work.




<CFQUERY NAME=&quot;GetWoord&quot; datasource=&quot;mydsn&quot; dbtype=&quot;ODBC&quot;>
SELECT *
FROM table
</CFQUERY>

<!--- Declare variable rec to sort and get the last record count --->

<cfset rec = Listlast(listsort(valuelist(GetWoord.id), &quot;numeric&quot;, &quot;asc&quot;))>

<!--- Run loop: one for every row outputed earlier --->

<cfloop index=&quot;i&quot; from=&quot;#rec#&quot; to=&quot;#form.RecordCount#&quot;>



<cfset locfield1 = &quot;form.field1&quot; & i>
<cfset locfield2 = &quot;form.field2&quot; & i>
<cfset locfield3 = &quot;form.field3&quot; & i>
<cfset locfield4 = &quot;form.field4&quot; & i>
..


<!--- All dynamic form variables are evaluated here . --->

<cfquery name=&quot;getwoord#i#&quot; datasource=&quot;mydsn&quot;>
INSERT INTO table
(field1, field2, field3, field4,... )
VALUES
('#Evaluate(locfield1)#', '#Evaluate(locfield2)#', '#Evaluate(locfield3)#',...)
</cfquery>

<!--- Loop to next row --->

</cfloop>


<!--- use CFLOCATION to return to mainform.cfm --->

<CFLOCATION URL=&quot;mainform.cfm&quot; addtoken=&quot;No&quot;>



 
What exactly are you trying to do with this? Spotting the error in the code is much easier when we know what you are trying to get it to do...

And what kind of error is it giving you? Please copy the error message and post it here.

Thanks
 
I think you need to change this line

<cfset locfield1 = &quot;form.field1&quot; & i>

to

<cfset locfield1 = evaluate(&quot;form.field1#i#&quot;)>

If that's not it, let us know more about the error you're getting or why it doesn't work.

Good luck,
GJ
 
GunJack, it looks like the evaluate function is being used in the query below #Evaluate(locfield1)#

If you think of the FORM scope as a structure, then you can get around using the processor intensive Evaluate() function.

The following examples yield the same results of FORM.myField1:

=== START CODE EXAMPLE ===
<cfset FORM.myField1 = &quot;Test&quot;>
<cfoutput>#FORM.myField1#</cfoutput>
=== END CODE EXAMPLE ===

=== START CODE EXAMPLE ===
<cfset FORM.myField1 = &quot;Test&quot;>
<cfoutput>#FORM
Code:
[
&quot;myField1&quot;
Code:
]
#</cfoutput>
=== END CODE EXAMPLE ===

=== START CODE EXAMPLE ===
<cfset FORM.myField1 = &quot;Test&quot;>
<cfoutput>#FORM
Code:
[
&quot;myField&quot; & 1
Code:
]
#</cfoutput>
=== END CODE EXAMPLE ===

=== START CODE EXAMPLE ===
<cfset FORM.myField1 = &quot;Test&quot;>
<cfset vTest = &quot;myField&quot;>
<cfoutput>
#FORM
Code:
[
vTest & 1
Code:
]
#
or
#FORM
Code:
[
&quot;#vTest#1&quot;
Code:
]
#
</cfoutput>
=== END CODE EXAMPLE ===

With that in mind, you can drop the <cfset> all together and make your code shorter by using the FORM structure in the query:

=== START CODE EXAMPLE ===
<cfloop index=&quot;i&quot; from=&quot;#rec#&quot; to=&quot;#form.RecordCount#&quot;>

<cfquery name=&quot;getwoord#i#&quot; datasource=&quot;mydsn&quot;>
INSERT INTO table
(field1,
field2,
field3,
field4,... )
VALUES
('#FORM
Code:
[
&quot;field1&quot; & i
Code:
]
#',
'#FORM
Code:
[
&quot;field2&quot; & i
Code:
]
#',
'#FORM
Code:
[
&quot;field3&quot; & i
Code:
]
#',...)
</cfquery>

</cfloop>
=== END CODE EXAMPLE ===


If this doesn't solve the problem, perhaps you could post the error. - tleish
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top