Skip to main content
Participating Frequently
June 6, 2008
Answered

Form with loop variable

  • June 6, 2008
  • 4 replies
  • 768 views
I have a form that has text boxes where the name is created dynamically with a loop variable.
<cfset formcounter = 1>
<cfoutput query="ViewParts">
<input type="hidden" name="RecordID#formcounter#" value="#RecordID#">
<cfset formcounter = formcounter + 1>
</cfoutput>
<cfoutput><cfset formcountermax = #formcounter#></cfoutput>

on the page it submits to I have:

<cfloop from="1" to="#formcountermax#" index="loopcount">
<cfif form.Qtyshipped1 NEQ "0" AND form.Qtyshipped1 NEQ "">
<CFQUERY NAME="InsertQty" DATASOURCE="mydatasource">
INSERT INTO InvoiceTable
(
InvoiceID,
QtyShipped,
RecordID
)
VALUES
(
'#GetLastInvID.InvoiceID#',
#form.Qtyshipped1#,
#form.RecordID1#
)
</CFQUERY>
</cfif>

Instead of the "1" at the end of the inserted variables, I want it to be my loopcount. I just don't know how to name it so that is possible.
    This topic has been closed for replies.
    Correct answer Newsgroup_User
    MarkWright wrote:

    > Any ideas?
    Don't use evaluate. 98.798% of the time it is unnecessary. Array
    notation, learn it, love it, it is very powerful.

    <cfset form['Qtyshipped' & loopcount] = 0>

    4 replies

    Newsgroup_UserCorrect answer
    Inspiring
    June 6, 2008
    MarkWright wrote:

    > Any ideas?
    Don't use evaluate. 98.798% of the time it is unnecessary. Array
    notation, learn it, love it, it is very powerful.

    <cfset form['Qtyshipped' & loopcount] = 0>
    Participating Frequently
    June 6, 2008
    That's exactly what I was looking for....array notation. EXACTLY. It's amazing how many google hits you go thru before you can find that. YEEESSS.
    Participating Frequently
    June 6, 2008
    since you got that one....let me ask something related.

    if evaluate('form.Qtyshipped' & '#loopcount#') = "" , I want to set that to 0.

    I tried:
    <cfset #evaluate('form.Qtyshipped' & '#loopcount#')# = 0>

    with and without # around the evaluate function, but I get the same error:

    Can not assign a value to a function.
    Unable to assign a value to the function "evaluate" on line ...

    Any ideas?

    Inspiring
    June 6, 2008
    There are two ways to make the previous answer more efficient. The first is to use array notation instead of evaluate.

    The 2nd, and you'll have to test it because sometimes it's less efficient, if to put the loop inside the query instead of having the query inside the loop. The syntax is

    insert into sometable
    (field1, field2, etc)
    <cfloop>
    select distinct value1, value2, etc
    from some_small_table
    <cfif loop not done>
    union
    </cfif>
    </cfloop>
    Participating Frequently
    June 6, 2008
    I just cut pieces of the actual code out and forgot the ending tag. Thanks for the help. It worked!

    June 6, 2008
    Mark, try using the values I placed below, I believe that should work. Also, I didnt see a close tag for your <cfloop> so I left it out as well.

    <cfloop from="1" to="#formcountermax#" index="loopcount">
    <cfif form.Qtyshipped1 NEQ "0" AND form.Qtyshipped1 NEQ "">
    <CFQUERY NAME="InsertQty" DATASOURCE="mydatasource">
    INSERT INTO InvoiceTable
    (
    InvoiceID,
    QtyShipped,
    RecordID
    )
    VALUES
    (
    '#GetLastInvID.InvoiceID#',
    '#evaluate('form.Qtyshipped' & '#loopcount#')#',
    '#evaluate('form.RecordID' & '#loopcount#')#'
    )
    </CFQUERY>
    </cfif>