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!

Shopping Cart Array - Remove Item

Status
Not open for further replies.

gi11ies

Programmer
Mar 26, 2002
52
Hi

I am trying to make a simple shopping cart, and am using a 5 element array for each item:

cartArray(0,intCartItem) = strProductName
cartArray(1,intCartItem) = strProductID
cartArray(2,intCartItem) = intProductPrice
cartArray(3,intCartItem) = intProductQuantity
cartArray(4,intCartItem) = intProductPrice * intProductQuantity

When I display the items in the cart, the table I display them in - I am displaying the quantity in a textbox:

<input type="text" name="<% =strQuantity %>" value="<% =cartArray(3,intCartItem) %>" size="4" maxlength="5">

also in the table for the cart, I have put in a checkbox for each item, for removing an item from the cart.

I want to have a button, that when its clicked - it will update the total price for product in the cart by multiplying the quantity by the product price - and also, if the check box for removing a product from the cart is checked, it will remove that product from the cart.

I have managed to create this functionality in ASP.NET, but jave to create it in Classic ASP also, and just can not get my head around t. The ASP.NET code I have used is:

Sub btnUpdate (sender as System.Object, e as System.EventArgs )
objDT = Session("Cart")
Dim dgItem as DataGridItem
Dim chkDel as System.Web.UI.WebControls.CheckBox
Dim i as Integer

For i = dg.Items.Count-1 To 0 Step -1
chkDel = CType(dg.Items(i).FindControl("itemCheck"), CheckBox)
If chkDel.checked then
objDT.Rows(i).Delete()
Else
objDt.Rows(i).Item("Quantity") = CType(dg.Items(i).FindControl("txtQuantity"), TextBox).Text
End If
Next

For Each objDR In objDT.Rows
objDR("Total") = objDR("Quantity") * objDR("Cost")
Next

Session("Cart") = objDT
dg.DataSource = objDT
dg.DataBind()
lblTotal.Text = GetItemTotal().ToString
cartView.visible = true
End Sub

If Anyone has any hints or tips on this, especially on how to remove an item from the array using a check box, it would be great !!!

Gillies
 
At first glance, it looks like your .NET code has a session variable named Cart that contains an object reference... and that the object has a property named .Rows that holds the array.

Are you using this same object in your ASP classic code or are you instead working on a way to mimic the functionality of the object?
 
It's totally new code I'm working with - Im not using the same object in Classic ASP, as I wasn't sure how to work around the DataGrid.

I'm just trying to get the same kind of functionality as I have- click on update button, checks if checkbox is checked, if yes then remove item from the cart, if no then multiply the item price by the value in the quantity text box.

Gillies
 
So the only data that is unique to each user is the actual contents of the array? ...and the other functionality can be abstracted to all users with an array?
 
Yes, thats it - the array gets its values from the previous page, a detail page of the item - and the values are passed through from the form.

In the above, I pasted code with each array element equal to a variable, these variables are set to the Request.QueryString("product_name") etc
 
The quick and ugly way to do it is with a 2 dimensional array and an include file that holds the functions that manipulate the array as a "cart"

Let me see if I can work something up for you as an example.
 
Here ya go. This is a class that I made that wraps a dictionary object instead of an array.

I liked the dictionary object better but I didnt want to stuff an object into a session variable... so the session data is stored in a 2 dimensional array and then inflated into the object when it is created and saved back into the session when destroyed. Hmm I hope that makes sense.


Anyway, this is clsCart.asp
Code:
<%
Class clsCartItem
	Public ID
	Public Price
	Public Quantity
End Class

Class clsCart
	'NOTE: The Dictionary that this class is based upon is Public so
	'      only the Subs to Load and Save it to the Session array
	'      are really needed... the others are included here for
	'      example and/or convience.
	
	Public CartItems
	
	Private Sub Class_Initialize()
		'Create instance of dictionary object
		Set CartItems = Server.CreateObject("Scripting.Dictionary")
	
		'Load dictionary from Session array
		IF IsArray(Session("cartArray")) THEN
			LoadCart Session("cartArray")
		END IF
	End Sub

	Private Sub Class_Terminate()
		SaveCart
		EmptyCart
		Set CartItems = Nothing
	End Sub
		
	
	Public Sub EmptyCart()
		'Simple wrapper to dictionary object's RemoveAll() method
		CartItems.RemoveAll 
	End Sub
	
	
	Public Sub LoadCart(arrCart)
		'Load dictionary from session array
	  Dim iCount
	  For iCount = LBound(arrCart, 2) To Ubound(arrCart, 2) 
			AddCartString arrCart(0, iCount), arrCart(1, iCount), arrCart(2, iCount)
	  Next
	End Sub


	Public Sub SaveCart()
	  'Load session array from dictionary
	  IF (CartItems.Count = 0) THEN
			Session("cartArray") = ""
			Exit Sub
		End IF

	  Dim TmpArray(), iCount, oItem
		Redim TmpArray(3, (CartItems.Count - 1))
		iCount = 0
		For Each oItem in CartItems
			TmpArray(0, iCount) = CartItems(oItem).ID
			TmpArray(1, iCount) = CartItems(oItem).Price
			TmpArray(2, iCount) = CartItems(oItem).Quantity			
			iCount = iCount + 1
		Next
		Session("cartArray") = TmpArray
	End Sub


	Public Property Get DistinctItemTypes()
		'Simple wrapper to dictionary object's .Count property
		DistinctItemTypes = CartItems.Count 
	End Property


	Public Property Get TotalPrice()
	  'Sum of price of all cart items
	  Dim oItem, curPrice
	  For Each oItem in CartItems
			curPrice = curPrice + (CartItems(oItem).Price * CartItems(oItem).Quantity)
	  Next
	  TotalPrice = curPrice
	End Property 

	
	Public Property Get TotalQuantity()
		'Sum of quantity of all cart items
	  Dim oItem, lNum
	  For Each oItem in CartItems
			lNum = lNum + CartItems(oItem).Quantity
	  Next
		TotalQuantity = lNum
	End Property


	Public Default Property Get CartItem(strID)
		Set CartItem = Nothing
		IF CartItems.Exists(strID) THEN
			Set CartItem = CartItems.Item(strID)
		END IF
	End Property
	
	
	Public Sub RemoveCartString(strID)
		IF CartItems.Exists(strID) THEN
			CartItems.Remove strID
		END IF	
	End Sub


	Public Sub RemoveCartObject(ob)
		IF NOT ValidCartItem(ob) THEN
			Err.Raise -2147221504, "clsCart", "Sub RemoveCartObject() [Bad Object Type - clsCartItem required]"
			Exit Sub
		END IF
		
		IF CartItems.Exists(ob.ID) THEN
			CartItems.Remove ob.ID
		END IF	
	End Sub


	Public Sub AddCartString(strID, curPrice, lQuantity)
		IF Not ValidInput(strID, curPrice, lQuantity) THEN
			Err.Raise -2147221504, "clsCart", "Sub AddCartString() [Bad input values]"
			Exit Sub
		END IF

		IF Not CartItems.Exists(strID) THEN
			'Add item to cart
			Dim oItem
			Set oItem = New clsCartItem
			oItem.ID = strID
			oItem.Price = curPrice
			oItem.Quantity = lQuantity
			CartItems.Add strID, oItem
		ELSE
			'Item is already in cart, increment the quantity
			CartItems.Item(strID).Quantity = lQuantity + CartItems.Item(strID).Quantity
		END IF
	End Sub


	Public Sub AddCartObject(ob)
		IF NOT ValidCartItem(ob) THEN
			Err.Raise -2147221504, "clsCart", "Sub AddCartObject() [Bad Object Type - clsCartItem required]"
			Exit Sub
		END IF
		
		IF Not ValidInput(ob.ID, ob.Price, ob.Quantity) THEN
			Err.Raise -2147221504, "clsCart", "Sub AddCartObject() [Bad input values]"
			Exit Sub
		END IF
		
		IF Not CartItems.Exists(ob.ID) THEN
			'Add item to cart
			CartItems.Add ob.ID, ob
		ELSE
			'Item is already in cart, increment the quantity
			CartItems.Item(ob.ID).Quantity = ob.Quantity + CartItems.Item(ob.ID).Quantity
		END IF
	End Sub


	Private Function ValidCartItem(ob)
		IF NOT IsObject(ob) THEN
			ValidCartItem = False
			Exit Function
		END IF
		
		'Dirty Hack because no TypeOf operator in VBScript
		On Error Resume Next
			
		ValidCartItem = ob.ID
		ValidCartItem = ob.Price
		ValidCartItem = ob.Quantity
			
		IF (Err.number <> 0) THEN
			ValidCartItem = False
		ELSE
			ValidCartItem = True
		END IF
			
		Err.Clear 
		On Error Goto 0
	End Function


  Private Function ValidInput(strID, curPrice, lQuantity)
		ValidInput = False
		
		IF (Len(strID) = 0) THEN Exit Function
		
		IF (Len(curPrice) = 0) THEN
			Exit Function
		ELSE
			IF NOT IsNumeric(curPrice) THEN Exit Function
		END IF
		
		IF (Len(lQuantity) = 0) THEN
			Exit Function
		ELSE
			IF NOT IsNumeric(lQuantity) THEN Exit Function
		END IF
		
		ValidInput = True
  End Function
End Class
%>

And here is a page that I made to test it.
Code:
<%@ Language = "VBScript" %>
<!-- #include file="clsCart.asp" -->
<%
Dim oCart
Set oCart = New clsCart

Response.Write "DistinctItemTypes in cart: " & oCart.DistinctItemTypes & "<BR>"

Response.Write "AddCartString <BR>"
oCart.AddCartString "foo", 5.32, 1
Response.Write "DistinctItemTypes in cart: " & oCart.DistinctItemTypes & "<BR>"


'Response.Write "EmptyCart <BR>"
'oCart.EmptyCart

Response.Write "DistinctItemTypes in cart: " & oCart.DistinctItemTypes & "<BR>"

Response.Write "AddCartObject <BR>"
dim poo
Set poo = New clsCartItem
poo.ID = "bar"
poo.Price = 3
poo.Quantity = 1
oCart.AddCartObject poo

Response.Write "DistinctItemTypes in cart: " & oCart.DistinctItemTypes & "<BR>"
Response.Write "Total Items: " & oCart.TotalQuantity & "<BR>"
Response.Write "Total Price: " & oCart.TotalPrice & "<BR>"
For each ob in oCart.CartItems
	Response.Write "Name: " & ob & " Price: " & oCart(ob).Price & " Qty: " & oCart(ob).Quantity & "<BR>"
Next

Set oCart = Nothing

%>
<br>
<br>
Hi, this is some plain text!

As you can see I didn't really test it all the way... there might be some bugs left in it.. wouldn't be surprised at all. The point here is just to maybe give you some ideas rather than to make the entire thing...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top