|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
I am building a shopping cart and I have a viewCart and a checkOut page, both are the same, except that the checkout page has "contact" information in it.
I'm working on the viewCart page and for some reason my Item Price is 9.99, but when I multiply it by the quantity, it registers at 9.00, like it is rounding down. Could someone help me find the error or find a way to fix it? Here is the code: Code:
<table border="1" cellpadding="3" cellspacing="1">
<tr>
<td>ItemID</td>
<td>Name</td>
<td>Episodes</td>
<td>Unit Price</td>
<td>Quantity</td>
<td>Total</td>
<td>Remove One </td>
</tr>
<%
Dim objDictionary, strKey, intValue, key, objConn, objRS, Price, Total, SubTotal, GrandTotal, strFind, adOpenStatic
adOpenStatic = 3
'Creates an ADO Connection object
Set objConn = Server.CreateObject("ADODB.Connection")
Set objRS = Server.CreateObject("ADODB.Recordset")
'Builds the connection to the datastore
objConn.Open strConnect
objRS.Open "Movies", objConn, adOpenStatic
If IsObject(Session("cart")) Then
Set objDictionary = Session("cart")
Else
Set objDictionary = CreateObject("Scripting.Dictionary")
End If
For Each key in objDictionary
strKey = key
intValue = objDictionary.Item(strKey)
strFind = "movieID = '" & strKey & "'"
objRS.Find strFind
If Int("0" & objDictionary(strKey)) <= intValue Then
Total = Int(objRS("itemPrice")) * intValue
Response.Write "<tr>"
Response.Write "<td>" & strKey & "</td>"
Response.Write "<td>" & objRS("itemName") & "</td>"
Response.Write "<td>" & objRS("Description") & "</td>"
Response.Write "<td>" & objRS("itemPrice") & "</td>"
Response.Write "<td>" & intValue & "</td>"
Response.Write "<td>" & FormatCurrency(Total) & "</td>"
Response.Write "<td><a href=""modifyCart.asp?action=remove&movieID=" & strKey & "&quantity=1"">Remove one!</a></td>"
Response.Write "</tr>"
SubTotal = SubTotal + Total
End If
Next
%>
</table>
</center>
<BR>
<table border="1" cellpadding="3" cellspacing="1" align="right">
<tr>
<td>
<p align="right">SubTotal: </td>
<td><%= SubTotal %> </td>
</tr>
<tr>
<td>
<p align="right">Shipping: </td>
<td>
<p align="right">$5.00</td>
</tr>
<% GrandTotal = subTotal + 5 %>
<tr>
<td>
<p align="right">Grand Total: </td>
<td><%= GrandTotal %> </td>
</tr>
</table>
|
|
#2
|
|||
|
|||
|
It looks like you are throwing the item price into an Int datatype...this gets rid of all decimals. Try using a Double instead.
|
|
#3
|
|||
|
|||
|
Sorry...I just got to thinking that I should have been more precise in my response. You are currently using Int which basically just chops of the fractional part of your number (IE - 9.99 goes to 9). You should change all of your Int() calls to CDbl(). ...And if need be you can use Round() or FormatNumber() to round out the number to 2 digits after the decimal.
|
|
#4
|
|||
|
|||
|
I can't believe easy of a fix that was. Maybe you could help me on my next problem.
When I click on Remove 1 from the cart, it works great. This morning if the quantity was at 1 and I clicked on remove 1, it would remove the whole row out of the table, but now it doesn't work anymore, it changes the quantity to zero. viewCart Code:
<%
Dim objDictionary, strKey, intValue, key, objConn, objRS, Price, Total, SubTotal, GrandTotal, strFind, adOpenStatic
adOpenStatic = 3
'Creates an ADO Connection object
Set objConn = Server.CreateObject("ADODB.Connection")
Set objRS = Server.CreateObject("ADODB.Recordset")
'Builds the connection to the datastore
objConn.Open strConnect
objRS.Open "Movies", objConn, adOpenStatic
If IsObject(Session("cart")) Then
Set objDictionary = Session("cart")
Else
Set objDictionary = CreateObject("Scripting.Dictionary")
End If
For Each key in objDictionary
strKey = key
intValue = CDbl(objDictionary.Item(strKey))
strFind = "movieID = '" & strKey & "'"
objRS.Find strFind
If CDbl("0" & objDictionary(strKey)) <= intValue Then
Total = CDbl(objRS("itemPrice")) * intValue
Response.Write "<tr>"
Response.Write "<td>" & strKey & "</td>"
Response.Write "<td>" & objRS("itemName") & "</td>"
Response.Write "<td>" & objRS("Description") & "</td>"
Response.Write "<td>" & FormatCurrency(objRS("itemPrice")) & "</td>"
Response.Write "<td>" & intValue & "</td>"
Response.Write "<td>" & FormatCurrency(Total) & "</td>"
Response.Write "<td><a href=""modifyCart.asp?action=remove&movieID=" & strKey & "&quantity=1"">Remove one!</a></td>"
Response.Write "</tr>"
SubTotal = SubTotal + Total
End If
Next
%>
modifyCart Code:
<%
Dim objDictionary, strAction, strKey, intValue
If IsObject(Session("cart")) Then
Set objDictionary = Session("cart")
Else
Set objDictionary = Server.CreateObject("Scripting.Dictionary")
End If
strAction = Request.QueryString("action")
strKey = Request.QueryString("movieID")
intValue = Int(Request.QueryString("quantity"))
Select Case strAction
Case "add"
AddItemToCart strKey, intValue
Case "remove"
RemoveItemFromCart strKey, intValue
End Select
intValue = objDictionary(strKey)
Set Session("cart") = objDictionary
Response.Redirect ("viewCart.asp")
%>
<%
Sub AddItemToCart(strKey, intValue)
If objDictionary.Exists(strKey) Then
objDictionary(strKey) = Int(objDictionary(strKey)) + intValue
Else
objDictionary.Add strKey, intValue
End If
Response.Write intValue
End Sub
%>
<%
Sub RemoveItemFromCart(strKey, intValue)
If objDictionary.Exists(strKey) Then
If Int(objDictionary(strKey)) <= 0 Then
objDictionary.Remove strKey
Else
objDictionary(strKey) = objDictionary(strKey) - 1
End If
End If
End Sub
%>
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > ASP Development > Help with ASP Shopping Cart |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|