slackie
asked on
inserting multiple items using cfform and cfloop
Hi. I have a catalog list of items on a single page, that are outputed by a simple query. It is a shopping cart, so I am trying to get the user to input the quantity they want to purchase of each item into an input box, and then submit the order with one order button at the bottom of the page. That then posts to an additems page that inserts the item number, quantity, and a few pre-set variables. Below is my code, and I am lost.
Order Page
----------
<form action="addtest.cfm" method="post" name="addtest">
<cfoutput query="getproducts">
<tr>
<td align="center"><cfif #getproducts.image# neq ""><td class="content">#getproduc ts.id#</td >
<td class="content">#getproduc ts.item#</ td>
<td class="content">#getproduc ts.size#</ td>
<td class="content">US#LSCurre ncyFormat( getproduct s.price, "local")#</td>
<td class="content"><input type="text" class="quantity" name="quantity" size="2"></td>
</tr>
<input type="hidden" name="storeid" value="#storeid#">
<input type="hidden" name="ID" value="#getproducts.id#">
<input type="hidden" name="customernumber" value="#sessionid#">
<tr>
<td colspan="6"><input type="submit" name="order" class="order" value="ORDER">
</td>
</tr>
</cfoutput>
</form>
So from this I want to update a table named cart with only the fields below. I know I should probably be using cfloop as there are multiple item id's and multiple quantities. Below is the update query I would use if it was a single entry I was trying to make. How do I insert the multiple items and the quantities entered into the cart table?
Update Page (addtest.cfm)
-----------
<CFQUERY NAME="UpdateOrder" connectstring="#applicatio n.connectS tring#" dbtype="DYNAMIC">
INSERT INTO cart (storeid, customer_id, itemnumber, itemquantity, dateentered)
VALUES (#storeid#, '#customernumber#', '#id#', #quantity#, #now()#)
</CFQUERY>
Order Page
----------
<form action="addtest.cfm" method="post" name="addtest">
<cfoutput query="getproducts">
<tr>
<td align="center"><cfif #getproducts.image# neq ""><td class="content">#getproduc
<td class="content">#getproduc
<td class="content">#getproduc
<td class="content">US#LSCurre
<td class="content"><input type="text" class="quantity" name="quantity" size="2"></td>
</tr>
<input type="hidden" name="storeid" value="#storeid#">
<input type="hidden" name="ID" value="#getproducts.id#">
<input type="hidden" name="customernumber" value="#sessionid#">
<tr>
<td colspan="6"><input type="submit" name="order" class="order" value="ORDER">
</td>
</tr>
</cfoutput>
</form>
So from this I want to update a table named cart with only the fields below. I know I should probably be using cfloop as there are multiple item id's and multiple quantities. Below is the update query I would use if it was a single entry I was trying to make. How do I insert the multiple items and the quantities entered into the cart table?
Update Page (addtest.cfm)
-----------
<CFQUERY NAME="UpdateOrder" connectstring="#applicatio
INSERT INTO cart (storeid, customer_id, itemnumber, itemquantity, dateentered)
VALUES (#storeid#, '#customernumber#', '#id#', #quantity#, #now()#)
</CFQUERY>
ASKER
Got the cart working to see if any of the items are already in there, so it just adds to the quantity rather than adding another record. check the working code below. However, I am stuck on the form validation to ensure there is 0 or more entered by the user. Ideas?
<cfloop from="1" to ="#ListLen(form.id)#" index="i">
<!--- Only work with quantities > 0 --->
<cfif ListGetAt(form.quantity,i) GT 0>
<!--- Check if there is another record in the cart table that contains the same
combination --->
<CFQUERY NAME="Lookup" connectstring="#applicatio n.connectS tring#" dbtype="DYNAMIC">
SELECT *
FROM cart
WHERE cart.ItemNumber = '#ListGetAt(form.id,i)#' AND
cart.StoreID = #ListGetAt(form.storeid,i) # AND
cart.Customer_ID = '#ListGetAt(form.customern umber,i)#'
</CFQUERY>
<CFSET exist = #Lookup.RecordCount#><!--- It's gonna be 0 or 1 --->
<!--- If there is no other record with the same combo --->
<CFIF #exist# eq 0>
<cfquery name="gogogo" connectstring="#applicatio n.connectS tring#" dbtype="DYNAMIC">
INSERT INTO cart (storeid, customer_id, itemnumber, itemquantity)
values (#ListGetAt(form.storeid,i )#, '#ListGetAt(form.customern umber,i)#' , '#ListGetAt(form.id,i)#',# ListGetAt( form.quant ity,i)#)</ cfquery>
<!--- Else, calculate the new quantity and update the cart table --->
<CFELSE>
<CFOUTPUT QUERY="Lookup">
<CFSET newquant = #ItemQuantity# + #ListGetAt(form.quantity,i )#>
</CFOUTPUT>
<CFQUERY NAME="UpdateOrder" connectstring="#applicatio n.connectS tring#" dbtype="DYNAMIC">
UPDATE cart
SET
ItemQuantity = #newquant#
WHERE cart.ItemNumber = '#ListGetAt(form.id,i)#' AND
cart.StoreID = #ListGetAt(form.storeid,i) # AND
cart.Customer_ID = '#ListGetAt(form.customern umber,i)#'
</CFQUERY>
</CFIF>
</cfif>
</cfloop>
<cfloop from="1" to ="#ListLen(form.id)#" index="i">
<!--- Only work with quantities > 0 --->
<cfif ListGetAt(form.quantity,i)
<!--- Check if there is another record in the cart table that contains the same
combination --->
<CFQUERY NAME="Lookup" connectstring="#applicatio
SELECT *
FROM cart
WHERE cart.ItemNumber = '#ListGetAt(form.id,i)#' AND
cart.StoreID = #ListGetAt(form.storeid,i)
cart.Customer_ID = '#ListGetAt(form.customern
</CFQUERY>
<CFSET exist = #Lookup.RecordCount#><!---
<!--- If there is no other record with the same combo --->
<CFIF #exist# eq 0>
<cfquery name="gogogo" connectstring="#applicatio
INSERT INTO cart (storeid, customer_id, itemnumber, itemquantity)
values (#ListGetAt(form.storeid,i
<!--- Else, calculate the new quantity and update the cart table --->
<CFELSE>
<CFOUTPUT QUERY="Lookup">
<CFSET newquant = #ItemQuantity# + #ListGetAt(form.quantity,i
</CFOUTPUT>
<CFQUERY NAME="UpdateOrder" connectstring="#applicatio
UPDATE cart
SET
ItemQuantity = #newquant#
WHERE cart.ItemNumber = '#ListGetAt(form.id,i)#' AND
cart.StoreID = #ListGetAt(form.storeid,i)
cart.Customer_ID = '#ListGetAt(form.customern
</CFQUERY>
</CFIF>
</cfif>
</cfloop>
Hi there,
3 comments for ur 3 Q's posted above ...
1. U need to run a loop to chk how many items the user has selected & insert them - one after another in the cart table. [if u got this workign gr8 !]
2. incase the users goes back & changes teh Qty - u have to just update the Qty & NOT insert another record.
[I see u have a insert statement - instead of an UPDATE - But if u got this workign gr8 !]
3. If u want to ensure the Qty amount to be greater than 0 - have a javascript validation & also put a server side validation.
ie inside the loop for "n" items
<cfloop>
<cfif "currentitem_Qty" GT 0>
insert the record
else continue with the loop
</cfif>
</cfloop>
let me know .. incase u need more help !
K'Rgds
Anand
3 comments for ur 3 Q's posted above ...
1. U need to run a loop to chk how many items the user has selected & insert them - one after another in the cart table. [if u got this workign gr8 !]
2. incase the users goes back & changes teh Qty - u have to just update the Qty & NOT insert another record.
[I see u have a insert statement - instead of an UPDATE - But if u got this workign gr8 !]
3. If u want to ensure the Qty amount to be greater than 0 - have a javascript validation & also put a server side validation.
ie inside the loop for "n" items
<cfloop>
<cfif "currentitem_Qty" GT 0>
insert the record
else continue with the loop
</cfif>
</cfloop>
let me know .. incase u need more help !
K'Rgds
Anand
No comment has been added lately, so it's time to clean up this question.
I will leave the following recommendation in the Cleanup topic area:
PAQ/No Refund
Please leave any comments here within the next four days.
mrichmon
EE Cleanup Volunteer
I will leave the following recommendation in the Cleanup topic area:
PAQ/No Refund
Please leave any comments here within the next four days.
mrichmon
EE Cleanup Volunteer
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
<cfquery name="gogogo" connectstring="#applicatio
INSERT INTO cart (storeid, customer_id, itemnumber, itemquantity)
values (#ListGetAt(form.storeid,i
</cfloop>