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

Looping through 2D array shopping cart

Status
Not open for further replies.

JanetB

Programmer
Oct 10, 2002
9
0
0
US
I am new to this and am in need of assistance. I need to loop through the shopping cart to look for duplicate stocknumbers. Desired result will be that the item quantity will be increased if stocknumber already exists in the shopping cart. Code to follow:
Code:
<cfloop collection=&quot;#FORM#&quot; ITEM=&quot;VarName&quot;>	
	<cfif VarName NEQ &quot;AddToCart&quot;><!--- nothing for submit form field --->
		<cfset stocknum = #VarName#>
		<cfset quantity = #FORM[VarName]#>
		
		<CFIF quantity NEQ &quot;&quot;><!--- if there is a quantity --->
		<cfquery name=&quot;qryGetItem&quot; datasource=&quot;landscape&quot; dbtype=&quot;ODBC&quot;><!--- get item info --->
				SELECT StockNum, Price, Detail, Note, ShipCode
				FROM Item 
				WHERE StockNum = '#stocknum#' 
			</cfquery>
<!--- get the length of the array and add one to it --->
				 
			<!--- then edit the current quantity  which is       session.cart[position][2]--->
	
	<!--- else (stocknum is different) --->
					<cfif #qryGetItem.RecordCount# GT 0>	<!--- if we got a record we'll add next --->	
					<cfset position = ArrayLen(session.cart) + 1> 
										
			<cfloop condition=&quot;#stocknum# EQ #session.cart[position][1]#&quot;>
			<cfset session.cart[position][2] = #FORM[VarName]# + #quantity#>
	
					<cfset session.cart[position][1] = &quot;#stocknum#&quot;><!--- stock number  --->
					<cfset session.cart[position][2] = &quot;#quantity#&quot;><!--- quantity --->
					<cfset session.cart[position][3] = &quot;#qryGetItem.price#&quot;>   <!--- price --->
					<cfset session.cart[position][4] = &quot;#qryGetItem.detail#&quot;><!--- description --->
					<cfset session.cart[position][5] = &quot;#qryGetItem.shipcode#&quot;><!--- shipcode --->
						<cfif IsDefined(&quot;qryGetItem.note&quot;)>
							<cfset session.cart[position][6] = &quot;#qryGetItem.note#&quot;><!--- note --->
						<cfelse>
							<cfset session.cart[position][6] = &quot; &quot;><!--- no note, make blank --->
						</cfif>
					<!--- set the total cost of all items --->
					<cfset current_cost = #qryGetItem.price# * #quantity#>
					<cfset session.RunningTotal = session.RunningTotal + current_cost> 
				</CFLOOP><!--- end of conditional loop --->
				</cfif><!--- end recordcount --->
	<!--- end of is stocknum is same --->			
				
		
		</cfif><!--- end of if quantity --->	
		
	</CFIF><!--- end if not submit --->	
</cfloop>	<!--- end of the loop of items to add --->
		<!--- send the user to the view page --->
<cflocation url=&quot;[URL unfurl="true"]http://10.1.2.22/shop/CartView.cfm&quot;[/URL] addtoken=&quot;YES&quot;>
THANK YOU!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top