472,353 Members | 2,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

help with using shopping cart totals

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
7 2503

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
>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
>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

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

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
>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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: laurie | last post by:
Hi all, I'm trying to help out a friend who has inherited a client with a PHP shopping cart application. Neither of us know PHP, but I've been...
2
by: Don Grover | last post by:
I am retrieving costs and product id's from a sql db. and need to build a shopping cart around it. How do I store the selected items and qty req so...
1
by: madison | last post by:
Hi, I am trying to start a website using paypals shopping cart function. If i have 10 items and they sell out, how do I make it so the item is...
1
by: Jia Sun | last post by:
hello , everybody , i need a similar program , just like fancyimport.com if possible, pls contact me ,thank you very much . inchina@gmail.com
17
by: Phil McKraken | last post by:
I am having a problem putting together a shopping cart with the below script. Everything displays fine, adds totals fine, and works perfect EXCEPT...
2
by: isaac2004 | last post by:
hi i am creating a basic asp site that uses cookies to manage a cart for an online store. whenever i open this page without adding anything to the...
3
by: isaac2004 | last post by:
hello i am making a spoof online book store site for a class and I was wondering how i could fix a problem i am having. I have two tables, one the...
0
by: samjam | last post by:
Below is some coding in a program i am using, i would like to know how i can get the text bigger or bolder on my webpage, This is the section of text...
3
by: Paulo | last post by:
Hi, beginner on asp.net 2.0 C# VS 2005, how can I use the shopping cart concept on my application? When the user clicks add item, it will be stored...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.