|
-
Jan 18th, 2005, 03:45 PM
#10
Thread Starter
Addicted Member
Re: Shopping Cart Woes...
okay, i've pretty much done my own version of the example on asp101. Here is the code that I have and I cannot for the life of me see the problem. The other issue that I'm having is that the cart won't add a quantity of a single item. In other words, if you run this code and add the same item over and over, it will add it as an individual item each time, which isn't supposed to happen. Any thoughts?
Thanks,
Jim P.
HTML Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!-- #include file="template.asp" -->
<%
Dim Cart
'Check to see if the cart exists. If not, then add one
If IsObject(Session("ShoppingCart")) Then
Set Cart = Session("ShoppingCart")
Else
Set Session("ShoppingCart") = Server.CreateObject("Scripting.Dictionary")
Set Cart = Session("ShoppingCart")
End If
'Add the item to the cart
Sub AddToCart(ItemID, ItemCount)
If Cart.Exists(ItemID) Then
Cart(ItemID) = Cart(ItemID) + ItemCount
Else
Cart.Add ItemID, ItemCount
End If
End Sub
'Remove the selected item from the cart. ***** NOT WORKING *****
Sub DeleteFromCart(ItemID, ItemCount)
Response.Write("This is the ItemID: " & ItemID & "<br>")
If Cart.Exists(ItemID) Then
If Cart(ItemID) <= ItemCount Then
Cart.Remove(ItemID)
Else
Cart(ItemID) = Cart(ItemID) - ItemCount
End If
Else
Response.Write("Can't find that item in your shopping cart.")
End If
End Sub
'Just simply for error checking...all keys and values seem to be right.
Sub CheckKeys()
For Each Key in Cart
Response.Write("<br>The value for key " & Key & " is " & Cart(Key) & "<br>")
Next
End Sub
'Show the contents of the cart
Sub ShowCart()
Dim cnConn, rsGetDetails
Dim dblSubTotal
dblSubTotal = 0
Set cnConn = Server.CreateObject("ADODB.Connection")
cnConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("online_wizard.mdb")
cnConn.CursorLocation = 3
Set rsGetDetails = Server.CreateObject("ADODB.Recordset")
Response.Write("<table width='60%' border='1' align='center'>")
Response.Write("<tr bgcolor='#FFFFFF'>")
Response.Write("<th>ISBN # / Item #</th>")
Response.Write("<th>Book Title</th>")
Response.Write("<th>Quantity</th>")
Response.Write("<th>Price</th>")
Response.Write("<th>Delete Item</th>")
Response.Write("</tr>")
For Each Key in Cart
rsGetDetails.Open "SELECT * FROM tblBooks WHERE ISBN = '" & Key & "'", cnConn
Response.Write("<tr>")
Response.Write("<td>" & Key & "</td>")
Response.Write("<td>" & rsGetDetails("BookTitle") & "</td>")
Response.Write("<td align='center'><input type='text' size='2' name='ItemCount' value='1'></td>")
Response.Write("<td>" & FormatCurrency(rsGetDetails("ListPrice")) & "</td>")
dblSubTotal = dblSubTotal + rsGetDetails("ListPrice")
Response.Write("<td><a href='cart_functions.asp?Action=delete&Key=" & Key & "&ItemCount=1'>Delete Item</a>")
Response.Write("</tr>")
rsGetDetails.Close
Next
Response.Write("<tr>")
Response.Write("<td colspan='5' align='center'><a href='cart_functions.asp?Action=clear'>Empty All Items From Cart</a></td>")
Response.Write("</tr>")
Response.Write("</table>")
Response.Write("<br><br>Your Subtotal Is: " & FormatCurrency(dblSubTotal))
Set rsGetDetails = Nothing
cnConn.Close
Set cnConn = Nothing
End Sub
'Select the action based on what has been selected by the user
Select Case Request.QueryString("Action")
Case "add"
AddToCart Request.QueryString("ItemID"), "1"
Case "delete"
DeleteFromCart Request.QueryString("Key"), Request.QueryString("ItemCount")
Case "clear"
Cart.RemoveAll
End Select
'Show the cart no matter what.
ShowCart
%>
"The Force will be with you, always."
--Ben Kenobi--
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|