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

Inserting Checkbox and Form Field into Table 2

Status
Not open for further replies.

Overmyhead2

Programmer
Jun 5, 2000
39
US
I have been able to insert either Item or Qty but not both.
A cfloop inside of a cfloop is not working..
How can I do a cfloop with 2 lists?
Here is my Action page..
Code:
<cfif IsDefined("Form.Item")>
<cfloop index="i" list="#form.Item#">
<cfif IsDefined("Form.Qty")>
<cfloop index="x" list="#form.Qty#">
<cfquery datasource="#Datasource#" name="insertItems">
INSERT INTO OrderItems(Qty, OrderDate, SiteID, ItemID)
VALUES('#x#','#SESSION.AUTH.OrderDate#', '#SESSION.AUTH.SiteID#', '#i#')
</cfquery>
</cfloop>
</cfif>
</cfloop>
</cfif>

Victoria
 
loops have scoped variables just like functions and such.

once you enter a new loop the index of the outside loop goes back to the first index.

I had to find out the hard way also.
thread232-742079

We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true.
 
I think i missunderstod what you're getting at a nested loop is going to give you a whol lot more results than you want.

if you're 100% sure each list index corasponds with the other you can do this.

<cfloop from = "1" to = "#listLen(form.item)# index = "i">
<cfquery datasource="#Datasource#" name="insertItems">
INSERT INTO OrderItems(Qty, OrderDate, SiteID, ItemID)
VALUES('#listGetAt(form.qty, i)#','#SESSION.AUTH.OrderDate#', '#SESSION.AUTH.SiteID#', '#listGetAt(form.item, i)#')
</cfquery>
</cfloop>

We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true.
 
hehe falconseye and i posted at the same time.

his solutionwill work if your qty fields are uniquely named

mine "should" work if all the qty text boxes are named the same and creates a list. The problem with using a list is there is nothing to garontee that each list index will match the other list. you could get mixed qty's for items.

We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true.
 
Thanks for the replies! I tried the sugested code untill it inserted something to the db. The items! are gonig in ok .. but the Qty is counting 1, 2, 3 ..incrementing for each row instead of the value of the form field Qty.
Here's my code
Code:
<cfquery name="OrderData" datasource="#DataSource#">
  SELECT  i.*
  From  ItemMaster i 
WHERE i.objcode=6341 
ORDER BY ItemID
</cfquery>
<TABLE>
<form method="post" action="ProcessOrder.cfm?ItemID=#URL.ITEMID#">
<TR>   <th>Order</th>
       <td>Quanity</td>
       <td>Item Code</td>
       <td>Description</td>
       <td>Unit</td>
</TR>    
<CFSET Counter=0>
<CFOUTPUT QUERY="OrderData">
  <TD><Input type="CheckBox" name="Item#Counter#" value="#ItemID#" </TD>	
  <TD><input name="Qty" type="text" value="" size="3"  maxlength="3">TD>
 	<TD>#ItemID#</TD>
 	 <TD>#Descr#</TD>   
	  <TD>#UnitMeas#</TD></TR> 
<CFSET counter=counter+1>
<input type="hidden" NAME=ObjectCode Value='6431'>
<input type="hidden" NAME='OrderDate' VALUE="#SESSION.AUTH.OrderDate#">
<input type="hidden" NAME='SiteID' VALUE="#SESSION.AUTH.SiteID#">
<input type="submit" value="Submit Order">
</CFOUTPUT></TABLE></form>
*****************************ACTION PAGE***********
<cfif IsDefined("Form.Qty")>
<cfloop index="i" from="1"to="#listLen(form.Qty)#">
<cfquery datasource="#Datasource#" name="insertItems">

INSERT INTO OrderItems(Qty, OrderDate, SiteID, ItemID)
VALUES(#i#,'#SESSION.AUTH.OrderDate#', '#SESSION.AUTH.SiteID#', #form['item'&i]#)

</cfquery>

</cfloop>
</cfif>

Victoria
 
that's because #i# is the index of the count 1,2,3,4
if qty is a list you want to use listgetat() function.

listGetAt(listName, place)
#listGetAt(form.qty, i)#

We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true.
 
oooo, i see what goes on, you are doing ListLen(form.Qty) instead try something like

<cfset i = 1
<cfif IsDefined("Form.Qty")>
<cfloop index="i" from="1"to="#listGetAt(FORM.Qty, i)#>
<cfset i = i + 1>
<cfquery datasource="#Datasource#" name="insertItems">

INSERT INTO OrderItems(Qty, OrderDate, SiteID, ItemID)
VALUES(#i#,'#SESSION.AUTH.OrderDate#', '#SESSION.AUTH.SiteID#', #form['item'&i]#)

</cfquery>

</cfloop>
</cfif>

hope it helps

 
YES!Thanks to both of you...This worked..
Code:
<cfif IsDefined("Form.Qty")>
<cfloop From = "1" To = "#ListLen(Qty)#" index = "Counter">
   <cfquery datasource="#Datasource#" name="insertItems">
INSERT INTO OrderItems(Qty, OrderDate, SiteID, ItemID)
VALUES(#ListGetAt(Qty, Counter)#,'#SESSION.AUTH.OrderDate#', '#SESSION.AUTH.SiteID#', #form['item'&counter]#)</cfquery>
</cfloop>

Victoria
 
sorry for my stupidity

<cfset i = 1> <!--- no need for this line --->
<cfif IsDefined("Form.Qty")>
<cfloop index="i" from="1"to="#listGetAt(FORM.Qty, i)#>
<cfset i = i + 1> <!--- no need for this line --->



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top