By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,719 Members | 1,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,719 IT Pros & Developers. It's quick & easy.

help with using shopping cart totals

P: n/a
hi i have a basic asp page that acts as an online bookstore. on my cart
page i am having trouble generating 3 numbers; a subtotal, a shipping
total, and a final price. here is my code

i would like it to work properly so that a record count counts through
all the books and genertates these numbers. watch out for line breaks

<%@ Language=VBScript %>
<% Option Explicit %>
<!--#include file="DatabaseConnect.asp"-->
<!--#include virtual="06winter/levini/database/adovbs.inc"-->
<html>
<head><title>Shopping Cart</title>
<link rel="stylesheet" href="BookStore.css" type="text/css">
</head>
<table>

<!-- Header SSI starts here-->
<!--#include file="header.html"-->
<!-- Header SSI ends here-->
<!-- Author SSI begins here-->
<!--#include file="ListAuthors.asp"-->
<!-- Author SSI ends here-->
<%
'This shopping cart uses a cookie named "ShoppingCart"
'to store the ISBNs of the items in the cart.
'The cookie puts each ISBN value into a separate key named ISBN1,
ISBN2, etc.
'The current number of items in the cart is stored in a key named
"ItemCount"

dim ISBN, iItemCount, iItem, iCount, bFound, curPrice, curDiscPrice,
strSQL, objRS, strTitle, dblPrice, dblShipping, dblFinalPrice,
dblTotalPrice

ISBN = trim(Request.QueryString("isbn"))

'sql statement for individual books
strSQL = "SELECT tblBookDescription.ISBN,
tblBookDescription.strTitle, tblBookDescription.strDescription,
tblBookDescription.strPublisher, tblBookDescription.dblPrice,
tblAuthors.AuthorID " & _
"FROM tblAuthors INNER JOIN (tblBookDescription INNER JOIN
tblAuthorsBooks ON tblBookDescription.ISBN = tblAuthorsBooks.ISBN) ON
tblAuthors.AuthorID = tblAuthorsBooks.AuthorID " & _
"WHERE (((tblBookDescription.ISBN)='" & ISBN & "')); "

'response.write("strSQL = " & strSQL)
Set objRS = Server.CreateObject("ADODB.Recordset")

objRS.open strSQL, objConn
curPrice = FormatCurrency(objRS("dblPrice"))
curDiscPrice = FormatCurrency((objRS("dblPrice")*.8))
dblPrice = (objRS("dblPrice"))

dblTotalPrice = dblTotalPrice + curDiscPrice

'dblShipping = 3.49 + (iItemCount - 1) * .99

dblFinalPrice = dblTotalPrice + dblShipping

'Initialize iItemCount with number of books in the cart
If request.cookies("ShoppingCart").HasKeys then
iItemCount = request.cookies("ShoppingCart")("ItemCount")
else
'No cookie yet
iItemCount = 0
end if

'Add items to cart
If Request("action") = "add" and len(ISBN)>5 then
'add new item to ShoppingCart
iItemCount = iItemCount + 1
Response.cookies("ShoppingCart")("ISBN"&iItemCount ) = ISBN
end if

'Delete items from Cart
If Request.QueryString("action") = "del" and len(ISBN)>5 then
'find the isbn
bFound = "False"
iItem = 1
Do while NOT bFound and iItem <= iItemCount
If Request.Cookies("ShoppingCart")("ISBN"&iItem) = ISBN then
bFound = "True"
else
iItem = iItem + 1
End If
Loop

If bFound then
'replace the deleted item with the item above it
iItemCount=iItemCount - 1
For iCount = iItem to iItemCount
Response.Cookies("ShoppingCart")("ISBN" &iCount)= _
Request.Cookies("ShoppingCart")("ISBN"&iCount+1)
Next
else
Response.write "Error: Could not match ISBN to delete."
end if
End If

'Update iItemCount
Response.Cookies("ShoppingCart")("ItemCount")=iIte mCount
Response.Cookies("ShoppingCart").Expires = Date + 30

'
************************************************** *********************************
' Thsi is the end of the cookie protion for the cart
' The Items are displayed under here
'
************************************************** *********************************

' list items in shopping cart
If iItemCount < 1 then
response.write "<center><font face='Comic Sans MS'
color='#FF0000'>" & _
"Your Shopping Cart is empty.</font><br><br>"
Else
%>
<div align="center">
<center>
<font face='Comic Sans MS' color='#FF0000'>You have <%
=iItemCount %>
book<% If iItemCount > 1 then response.write "s"%> in
your shopping cart.
</font><br><br>

<table border="0" cellpadding="5" cellspacing="0"
width="643">
<tr>
<td width="20" > &nbsp; </td>
<td width="256" ><font face="Comic Sans MS"
Book</font></td> <td align="center" width="66" ><font face="Comic Sans MS"Quantity</font></td> <td width="179" ><font face="Comic Sans MS"Price</font></td> <td width="62" ><font face="Comic Sans MS"Remove</font></td>

</tr>
<%
'List each item in cart
For iCount = 1 to iItemCount
ISBN =
Request.Cookies("ShoppingCart")("ISBN"&iCount)
%>

<tr>
<td valign="top" width="20" >1.</td>
<td valign="top" width="256">
<! Book Title, author, stock >
<a href="ProductPage.asp?isbn= <% =objRS("ISBN")%> ">
<% =objRS("strTitle")%></a>
<br>
<font size="-1">
by <% =funListAuthors(objRS("ISBN")) %></a>
</font>
<br>
</td>
<td valign="top" align="center" width="66">1</td>
<td valign="top" width="179">
<! Price >
<FONT face=arial,verdana,helvetica><B>List Price:
<font color=#990000><strike><% =dblPrice
%></strike></font><br>

<FONT face=arial,verdana,helvetica>Our Price:
<font color=#990000><% =curDiscPrice %></font><br>

<FONT face=arial,verdana,helvetica>You Save:
<font color=#990000><% =FormatCurrency((dblPrice -
curDiscPrice)) %>(20%)</b></font><br><br>
</td>
<td valign="top" width="62" >
<a
href="ShoppingCart.asp?ISBN=<%=objRS("ISBN")%>&act ion=del"> Remove</a>
</td>
</tr>

<% Next
%> </table>
</center>
<%End If
%>
<tr>
<td></td><td></td><td></td>
<td>
<FONT face=arial,verdana,helvetica>
<table border="0" cellpadding="0" cellspacing="0"
width="150" >
<tr>

------------------------------------------------------problem area
<td width="75">Sub-Total:</td>
<td width="75"><p align="right">$152.00</td>
</tr>

<tr>
<td width="75">Shipping*:</td>
<td width="75"><p align="right"><% =dblShipping
%></td>
</tr>
<tr>
<td width="75">Total:</td>
<td width="75"><p align="right"><%
=dblFinalPrice%></td>
</tr>
------------------------------------------------------------------------------------------

</table>
</td>
</tr>
</table>
</center>
</div>

</table>
<br>
<div align="center">
<center>
<table border="0" cellpadding="5" cellspacing="0" width="400"
bgcolor="#99CCFF">
<tr>
<td><a href="default.asp"><img border="0"
src="/06Winter/levini/images/continue-shopping.gif" width="121"
height="19"></a><br>
</td>
<td>
<p align="right"><a href="checkout.asp"><img border="0"
src="/06Winter/levini/images/proceed-to-checkout.gif" width="183"
height="31"></a>
</td>
</tr>
</table>

<table border="0" cellpadding="5" cellspacing="0" width="400">
<tr>

-------------------------------------------------shipping guidlines
<td> <br><p align=center> Shipping is $3.49 for the first book
and $.99 for each additional book. To assure reliable delivery and
to keep your costs low we send all books via UPS ground.
------------------------------------------------------------------------------------------------

</td>
</tr>
</table>
</body>
</html>
i know cookies isnt an ideal way to do it but i dont really know how to
do it with a SQL statement any help for any of this would be great.
thank you

isaac

Feb 24 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a

isaac2004 wrote:
hi i have a basic asp page that acts as an online bookstore. on my cart
page i am having trouble generating 3 numbers; a subtotal, a shipping
total, and a final price. here is my code

i would like it to work properly so that a record count counts through
all the books and genertates these numbers. watch out for line breaks

<%@ Language=VBScript %>
<% Option Explicit %>
<!--#include file="DatabaseConnect.asp"-->
<!--#include virtual="06winter/levini/database/adovbs.inc"-->
<html>
<head><title>Shopping Cart</title>
<link rel="stylesheet" href="BookStore.css" type="text/css">
</head>
<table>

<!-- Header SSI starts here-->
<!--#include file="header.html"-->
<!-- Header SSI ends here-->
<!-- Author SSI begins here-->
<!--#include file="ListAuthors.asp"-->
<!-- Author SSI ends here-->
<%
'This shopping cart uses a cookie named "ShoppingCart"
'to store the ISBNs of the items in the cart.
'The cookie puts each ISBN value into a separate key named ISBN1,
ISBN2, etc.
'The current number of items in the cart is stored in a key named
"ItemCount"

dim ISBN, iItemCount, iItem, iCount, bFound, curPrice, curDiscPrice,
strSQL, objRS, strTitle, dblPrice, dblShipping, dblFinalPrice,
dblTotalPrice

ISBN = trim(Request.QueryString("isbn"))

'sql statement for individual books
strSQL = "SELECT tblBookDescription.ISBN,
tblBookDescription.strTitle, tblBookDescription.strDescription,
tblBookDescription.strPublisher, tblBookDescription.dblPrice,
tblAuthors.AuthorID " & _
"FROM tblAuthors INNER JOIN (tblBookDescription INNER JOIN
tblAuthorsBooks ON tblBookDescription.ISBN = tblAuthorsBooks.ISBN) ON
tblAuthors.AuthorID = tblAuthorsBooks.AuthorID " & _
"WHERE (((tblBookDescription.ISBN)='" & ISBN & "')); "

'response.write("strSQL = " & strSQL)
Set objRS = Server.CreateObject("ADODB.Recordset")

objRS.open strSQL, objConn
curPrice = FormatCurrency(objRS("dblPrice"))
curDiscPrice = FormatCurrency((objRS("dblPrice")*.8))
dblPrice = (objRS("dblPrice"))

dblTotalPrice = dblTotalPrice + curDiscPrice

'dblShipping = 3.49 + (iItemCount - 1) * .99

dblFinalPrice = dblTotalPrice + dblShipping

'Initialize iItemCount with number of books in the cart
If request.cookies("ShoppingCart").HasKeys then
iItemCount = request.cookies("ShoppingCart")("ItemCount")
else
'No cookie yet
iItemCount = 0
end if

'Add items to cart
If Request("action") = "add" and len(ISBN)>5 then
'add new item to ShoppingCart
iItemCount = iItemCount + 1
Response.cookies("ShoppingCart")("ISBN"&iItemCount ) = ISBN
end if

'Delete items from Cart
If Request.QueryString("action") = "del" and len(ISBN)>5 then
'find the isbn
bFound = "False"
iItem = 1
Do while NOT bFound and iItem <= iItemCount
If Request.Cookies("ShoppingCart")("ISBN"&iItem) = ISBN then
bFound = "True"
else
iItem = iItem + 1
End If
Loop

If bFound then
'replace the deleted item with the item above it
iItemCount=iItemCount - 1
For iCount = iItem to iItemCount
Response.Cookies("ShoppingCart")("ISBN" &iCount)= _
Request.Cookies("ShoppingCart")("ISBN"&iCount+1)
Next
else
Response.write "Error: Could not match ISBN to delete."
end if
End If

'Update iItemCount
Response.Cookies("ShoppingCart")("ItemCount")=iIte mCount
Response.Cookies("ShoppingCart").Expires = Date + 30

'
************************************************** *********************************
' Thsi is the end of the cookie protion for the cart
' The Items are displayed under here
'
************************************************** *********************************

' list items in shopping cart
If iItemCount < 1 then
response.write "<center><font face='Comic Sans MS'
color='#FF0000'>" & _
"Your Shopping Cart is empty.</font><br><br>"
Else
%>
<div align="center">
<center>
<font face='Comic Sans MS' color='#FF0000'>You have <%
=iItemCount %>
book<% If iItemCount > 1 then response.write "s"%> in
your shopping cart.
</font><br><br>

<table border="0" cellpadding="5" cellspacing="0"
width="643">
<tr>
<td width="20" > &nbsp; </td>
<td width="256" ><font face="Comic Sans MS"
Book</font></td>

<td align="center" width="66" ><font face="Comic Sans MS"
Quantity</font></td>

<td width="179" ><font face="Comic Sans MS"
Price</font></td>

<td width="62" ><font face="Comic Sans MS"
Remove</font></td>

</tr>
<%
'List each item in cart
For iCount = 1 to iItemCount
ISBN =
Request.Cookies("ShoppingCart")("ISBN"&iCount)
%>

<tr>
<td valign="top" width="20" >1.</td>
<td valign="top" width="256">
<! Book Title, author, stock >
<a href="ProductPage.asp?isbn= <% =objRS("ISBN")%> ">
<% =objRS("strTitle")%></a>
<br>
<font size="-1">
by <% =funListAuthors(objRS("ISBN")) %></a>
</font>
<br>
</td>
<td valign="top" align="center" width="66">1</td>
<td valign="top" width="179">
<! Price >
<FONT face=arial,verdana,helvetica><B>List Price:
<font color=#990000><strike><% =dblPrice
%></strike></font><br>

<FONT face=arial,verdana,helvetica>Our Price:
<font color=#990000><% =curDiscPrice %></font><br>

<FONT face=arial,verdana,helvetica>You Save:
<font color=#990000><% =FormatCurrency((dblPrice -
curDiscPrice)) %>(20%)</b></font><br><br>
</td>
<td valign="top" width="62" >
<a
href="ShoppingCart.asp?ISBN=<%=objRS("ISBN")%>&act ion=del"> Remove</a>
</td>
</tr>

<% Next
%> </table>
</center>
<%End If
%>
<tr>
<td></td><td></td><td></td>
<td>
<FONT face=arial,verdana,helvetica>
<table border="0" cellpadding="0" cellspacing="0"
width="150" >
<tr>

------------------------------------------------------problem area
<td width="75">Sub-Total:</td>
<td width="75"><p align="right">$152.00</td>
</tr>

<tr>
<td width="75">Shipping*:</td>
<td width="75"><p align="right"><% =dblShipping
%></td>
</tr>
<tr>
<td width="75">Total:</td>
<td width="75"><p align="right"><%
=dblFinalPrice%></td>
</tr>
------------------------------------------------------------------------------------------

</table>
</td>
</tr>
</table>
</center>
</div>

</table>
<br>
<div align="center">
<center>
<table border="0" cellpadding="5" cellspacing="0" width="400"
bgcolor="#99CCFF">
<tr>
<td><a href="default.asp"><img border="0"
src="/06Winter/levini/images/continue-shopping.gif" width="121"
height="19"></a><br>
</td>
<td>
<p align="right"><a href="checkout.asp"><img border="0"
src="/06Winter/levini/images/proceed-to-checkout.gif" width="183"
height="31"></a>
</td>
</tr>
</table>

<table border="0" cellpadding="5" cellspacing="0" width="400">
<tr>

-------------------------------------------------shipping guidlines
<td> <br><p align=center> Shipping is $3.49 for the first book
and $.99 for each additional book. To assure reliable delivery and
to keep your costs low we send all books via UPS ground.
------------------------------------------------------------------------------------------------

</td>
</tr>
</table>
</body>
</html>
i know cookies isnt an ideal way to do it but i dont really know how to
do it with a SQL statement any help for any of this would be great.
thank you

isaac


For each title that's added to the cart, store the price in another key
in the cookie ("ISBNPrice" & iCount). Then loop through them, adding
them as you go to obtain the subtotal:

dblSubTotal = 0
for i = 1 to iItemCount
dblSubTotal = dblSubTotal +
Request.Cookies("ShoppingCart")("ISBNPrice"&iCount )
next

You shipping price is as follows:

dblShipping = 3.49 + ((iItemCount - 1) * .99)

and your total price is dblShipping + dblSubTotal.

But you are right - cookies are not the ideal way to manage shopping
carts. They rely on the visitor having them enabled. You may want to
consider using session variables instead, or even writing the shopping
cart information to a database.

/P.

Feb 25 '06 #2

P: n/a
>But you are right - cookies are not the ideal way to manage shopping
carts. They rely on the visitor having them enabled. You may want to
consider using session variables instead, or even writing the shopping
cart information to a database.

i am not that familar with SQL so can i have a little help with the
syntax for writing it to a database because that would be ideal.

Feb 25 '06 #3

P: n/a
>dblSubTotal = 0
for i = 1 to iItemCount
dblSubTotal = dblSubTotal +
Request.Cookies("ShoppingCart")("ISBNPrice"&iCount )
next

im having trouble setting up this loop as well where would i put it in
my code

Feb 25 '06 #4

P: n/a

isaac2004 wrote:
dblSubTotal = 0

for i = 1 to iItemCount
dblSubTotal = dblSubTotal +
Request.Cookies("ShoppingCart")("ISBNPrice"&iCount )
next

im having trouble setting up this loop as well where would i put it in
my code


Where did you try to put it and what was the error?

/P.

Feb 25 '06 #5

P: n/a

isaac2004 wrote:
But you are right - cookies are not the ideal way to manage shopping

carts. They rely on the visitor having them enabled. You may want to
consider using session variables instead, or even writing the shopping
cart information to a database.

i am not that familar with SQL so can i have a little help with the
syntax for writing it to a database because that would be ideal


The best way to do it is to use a saved parameter query. Bob has
posted huge amounts about them. Here are some links:

http://groups.google.com/group/micro...UTF-8&oe=UTF-8
http://groups-beta.google.com/group/...d322b882a604bd
http://groups.google.com/group/micro...e36562fee7804e

/P.

Feb 25 '06 #6

P: n/a
>Where did you try to put it and what was the error?

there was no error it just didnt read the iItemCount, i put it right
before the prompt for the dblsubtotal.

Feb 25 '06 #7

P: n/a

isaac2004 wrote:
Where did you try to put it and what was the error?


there was no error it just didnt read the iItemCount, i put it right
before the prompt for the dblsubtotal.


Did you check the cookie to see if there was a value for iItemCount?
Are you adding the price to the cookie?

Also, your html is in such a mess that it's always possible the thing
you expect to see isn't displaying correctly. I've copied your code
into Dreamweaver, and chunks of it are hidden from view because of
overlapping and unclosed tags.

/P.

Feb 25 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.