|
-
Jan 18th, 2005, 03:45 PM
#1
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--
-
Jan 18th, 2005, 07:24 PM
#2
Re: Shopping Cart Woes...
Just tested your code, it seems like the problem is arising because you are using the key as a number. Add a charecter before the key e.g "ItemID" & Request.QueryString("ItemID"). That will resolve the problem.
VB Code:
Select Case Request.QueryString("Action")
Case "add"
AddToCart "ID" & Request.QueryString("ItemID"), "1"
Case "delete"
DeleteFromCart "ID" & Request.QueryString("Key"), Request.QueryString("ItemCount")
Case "clear"
Cart.RemoveAll
End Select
[VBF RSS Feed]
There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.
If I have been helpful, Please Rate my Post. Thanks.
This post was powered by : 
-
Jan 19th, 2005, 08:32 AM
#3
Thread Starter
Addicted Member
Re: Shopping Cart Woes...
Danial,
Thanks once again for all your help, but it still is not working. I changed the code as you suggested and now it's adding two "ID"'s at the beginning of the string and it's still not finding it...still says can't find that item in your cart. Could this be some kind of configuration problem with the server itself? I'm running win2k3 and IIS6. I wouldn't think that would be the problem, but you never know?
Thanks again for all your help.
Jim P.
"The Force will be with you, always."
--Ben Kenobi--
-
Jan 19th, 2005, 08:58 AM
#4
Re: Shopping Cart Woes...
 Originally Posted by jpiller
Danial,
Thanks once again for all your help, but it still is not working. I changed the code as you suggested and now it's adding two "ID"'s at the beginning of the string and it's still not finding it...still says can't find that item in your cart. Could this be some kind of configuration problem with the server itself? I'm running win2k3 and IIS6. I wouldn't think that would be the problem, but you never know?
Thanks again for all your help.
Jim P.
No, its not working because your DeleteFromCart function is coded incorrectly. Here is the correct version. I have highlighted the secion in red. As you can see you need to convert a value to a number before you can do <= comparision. All variable in VBScript are Variant so you need to convert them to integer (cInt) or any other numerical form if you want to do numerical comparison.
Hope this helps.
'
VB Code:
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
[COLOR=Red]If cInt(Cart(ItemID)) <= cInt(ItemCount) Then[/COLOR]
Cart.Remove(ItemID)
Response.write "Item Removed"
Else
Cart(ItemID) = Cart(ItemID) - ItemCount
Response.write "Count Decreased"
End If
Else
Response.Write("Can't find that item in your shopping cart.")
End If
End Sub
[VBF RSS Feed]
There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.
If I have been helpful, Please Rate my Post. Thanks.
This post was powered by : 
-
Jan 19th, 2005, 02:50 PM
#5
Thread Starter
Addicted Member
Re: Shopping Cart Woes...
Danial,
The script isn't working b/c the object that is in the cart isn't being seen by the Cart.Remove statement. It keeps saying that it doesn't find that object. The if statement you highlighted simply decides whether or not to delete all of that particular item or just the number specified by the user.
What I don't understand is why in the heck the item is showing up in the cart when I display it, it shows up when I loop through it, but when I go to delete the item, it doesn't see it. There must be something I'm doing wrong and I cannot figure out what it is....
Jim P.
"The Force will be with you, always."
--Ben Kenobi--
-
Jan 20th, 2005, 10:32 AM
#6
Thread Starter
Addicted Member
Figured out the issue....
Of all the stupid things....None of the documentation that I read and none of the examples that I worked with explained this, but here's the issue. In my original code, the DeleteFromCart Sub read like this:
HTML Code:
Sub DeleteFromCart()
If Cart.Exists(Request.QueryString("Key")) Then
Cart.Remove Request.QueryString("Key")
Else
Response.Write("Can't find that item in your shopping cart.")
End If
End Sub
The solution reads like this:
Sub DeleteFromCart()
If Cart.Exists(CStr(Request.QueryString("Key"))) Then
Cart.Remove CStr(Request.QueryString("Key"))
Else
Response.Write("Can't find that item in your shopping cart.")
End If
End Sub
When using Remove, your "Key" must be in string format, and b/c in ASP all variables are written as variant, I had to add the CStr function to convert it to a string.
Thanks Danial for all your help!
Jim P.
"The Force will be with you, always."
--Ben Kenobi--
-
Jan 20th, 2005, 09:01 PM
#7
Re: Figured out the issue....
 Originally Posted by jpiller
Of all the stupid things....None of the documentation that I read and none of the examples that I worked with explained this, but here's the issue. In my original code, the DeleteFromCart Sub read like this:
When using Remove, your "Key" must be in string format, and b/c in ASP all variables are written as variant, I had to add the CStr function to convert it to a string.
Thanks Danial for all your help!
Jim P.
 Originally Posted by Danial
All variable in VBScript are Variant so you need to convert them to integer (cInt) or any other numerical form if you want to do numerical comparison.
Well I did kinda warn you about the variable type .
Yes its good to work with a strong typed, but in your case your Delete function you needed to convert the ItevCount to integer for it to work. You do no need to use cstr to convert it to a string.
Glad to know your problem is resolved.
Good luck and happy learning
[VBF RSS Feed]
There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.
If I have been helpful, Please Rate my Post. Thanks.
This post was powered by : 
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
|