Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

please help! shopping cart

Status
Not open for further replies.

tuam1234

Technical User
Feb 6, 2004
52
IE
could someone please please help. i got a shopping cart off the net. it is one where the have static information about the items included in the code..

problem is, i want to change this static information to information taken from database. i have recked my head for the last 4 hours trying to do this and nothing!!!!

could someone please look at the code and tell me what i need to do !!! i know its alot to ask but it would be VERY VERY MUCH APPRECIATED.

this is my code that i have that gets info from db and puts into a table...

<%
'Dimension variables
Dim connection 'Holds the Database Connection Object
Dim check 'Holds the recordset for the records in the database
Dim data 'Holds the SQL query for the database
Dim price
Dim arrtime
Dim destination
Dim deptime
Dim leaving
Dim iItemID
Dim cost


Set connection = Server.CreateObject("ADODB.Connection")
Call connection.Open ("auction1")
Set data = Server.CreateObject("ADODB.Recordset")
check = "SELECT * FROM Holidays WHERE Destination = '"_
& CStr (Request ( "places")) & "'" _
& " AND Leaving = '"_
& CStr (Request ( "from")) & "'" _
& " AND arrTime = '"_
& CStr (Request ( "arr")) & "'"_
& " AND depTime = '"_
& CStr (Request ( "dep")) & "'"
data.Open check, connection
cost = (data("Price"))
'<table border=1><tr><td><strong>Price</strong></td></tr>
'<% While Not data.EOF
'Wend %>

</table>
<table border=1><tr><td><strong>Flight ID</strong></td><td><strong>Departing From</strong></td><td><strong>Price</strong></td><td><strong>Arrival Time</strong></td><td><strong>Departure Time</strong></td></tr>
<% While Not data.EOF
Response.Write("<tr><td>")
%><%=(data.Fields.Item("iItemID").Value)%><%
Response.Write("</td><td>")
%><%=(data.Fields.Item("Leaving").Value)%><%
Response.Write("</td><td>") 'Don't Finish the row yet
%><%=(data.Fields.Item("Price").Value)%><% 'Put in a column with some 'more data
Response.Write("</td><td>")
%><%=(data.Fields.Item("arrTime").Value)%><%
Response.Write("</td><td>")
%><%=(data.Fields.Item("depTime").Value)%><%

data.MoveNext
Wend %>


this is the shopping cart code....

Site Search Web Search
Webmaster Resource Index
Need Help With
Your Web Project? Development Marketing Management Consultation Quote Request

Back: Home: ASP Shopping Cart

<%

' This function is written to enable the adding of multiples of an item
' but this sample always just adds one. If you wish to add different
' quantities simply replace the value of the Querystring parameter count.
' We didn't do this because we wanted to keep the whole thing simple and
' not get into using forms so it stayed relatively readable.

Sub AddItemToCart(iItemID, iItemCount)
If dictCart.Exists(iItemID) Then
dictCart(iItemID) = dictCart(iItemID) + iItemCount
Else
dictCart.Add iItemID, iItemCount
End If
Response.Write iItemCount & " of item # " & iItemID & " have been added to your cart.<BR><BR>" & vbCrLf
End Sub

Sub RemoveItemFromCart(iItemID, iItemCount)
If dictCart.Exists(iItemID) Then
If dictCart(iItemID) <= iItemCount Then
dictCart.Remove iItemID
Else
dictCart(iItemID) = dictCart(iItemID) - iItemCount
End If
Response.Write iItemCount & " of item # " & iItemID & " have been removed from your cart.<BR><BR>" & vbCrLf
Else
Response.Write "Couldn't find any of that item your cart.<BR><BR>" & vbCrLf
End If
End Sub

Sub ShowItemsInCart()
Dim Key
Dim aParameters ' as Variant (Array)
Dim sTotal, sShipping

%>
<TABLE Border=1 CellPadding=3 CellSpacing=1>
<TR>
<TD>Item #</TD>
<TD>Description</TD>
<TD>Quantity</TD>
<TD>Remove Item From Cart</TD>
<TD>Price</TD>
<TD>Totals</TD>
</TR>
<%
sTotal = 0
For Each Key in dictCart
aParameters = GetItemParameters(Key)
%>
<TR>
<TD ALIGN="Center"><%= Key %></TD>
<TD ALIGN="Left"><%= aParameters(1) %></TD>
<TD ALIGN="Center"><%= dictCart(Key) %></TD>
<TD ALIGN="Left"><A HREF="./shopping.asp?action=del&item=<%= Key %>&count=1">Remove One</A>&nbsp;&nbsp;<A HREF="./shopping.asp?action=del&item=<%= Key %>&count=<%= dictCart(Key) %>">Remove All</A></TD>
<TD ALIGN="Right"><%= aParameters(2) %>$</TD>
<TD ALIGN="Right">$<%= FormatNumber(dictCart(Key) * CSng(aParameters(2)),2) %></TD>
</TR>
<%
sTotal = sTotal + (dictCart(Key) * CSng(aParameters(2)))
Next

'Calculate shipping - you might want to pull this out into a function if your shipping
' calculations are more complicated then ours. ;)
If sTotal <> 0 Then
sShipping = 7.5
Else
sShipping = 0
End If
sTotal = sTotal + sShipping
%>
<TR><TD COLSPAN=5 ALIGN="Right"><B>S+H:</B></TD><TD ALIGN="Right">$<%= FormatNumber(sShipping,2) %></TD></TR>
<TR><TD COLSPAN=5 ALIGN="Right"><B>Total:</B></TD><TD ALIGN="Right">$<%= FormatNumber(sTotal,2) %></TD></TR>
</TABLE>
<%
End Sub

Sub ShowFullCatalog()
Dim aParameters ' as Variant (Array)
Dim I
Dim iItemCount ' Number of items we sell
' If you are really going to use this sample this should probably be pulled from a DB
iItemCount =
%>
<TABLE Border=1 CellPadding=3 CellSpacing=1>
<TR>
<TD>Image</TD>
<TD>Description</TD>
<TD>Price</TD>
<TD>Add Item To Cart</TD>
</TR>
<%
For I = 1 to iItemCount
aParameters = GetItemParameters(I)
%>
<TR>
<TD><IMG SRC="<%= aParameters(0) %>"></TD>
<TD><%= aParameters(1) %></TD>
<TD>$<%= aParameters(2) %></TD>
<TD><A HREF="./shopping.asp?action=add&item=<%= I %>&count=1">Add this to my cart!</A></TD>
</TR>
<%
Next 'I
%>
</TABLE>
<%
End Sub
'
Sub PlaceOrder()
Dim Key
Dim aParameters ' as Variant (Array)
Dim sTotal, sShipping

%>
<TABLE Border=1 CellPadding=3 CellSpacing=1>
<TR>
<TD>Item #</TD>
<TD>Description</TD>
<TD>Quantity</TD>
<TD>Price</TD>
<TD>Totals</TD>
</TR>
<%
sTotal = 0
For Each Key in dictCart
aParameters = GetItemParameters(Key)
%>
<TR>
<TD ALIGN="Center"><%= Key %></TD>
<TD ALIGN="Left"><%= aParameters(1) %></TD>
<TD ALIGN="Center"><%= dictCart(Key) %></TD>
<TD ALIGN="Right">$<%= aParameters(2) %></TD>
<TD ALIGN="Right">$<%= FormatNumber(dictCart(Key) * CSng(aParameters(2)),2) %></TD>
</TR>
<%
sTotal = sTotal + (dictCart(Key) * CSng(aParameters(2)))
Next

'Calculate shipping - you might want to pull this out into a function if your shipping
' calculations are more complicated then ours. ;)
If sTotal <> 0 Then
sShipping = 7.5
Else
sShipping = 0
End If
sTotal = sTotal + sShipping
%>
<TR><TD COLSPAN=4 ALIGN="Right"><B>S+H:</B></TD><TD ALIGN="Right">$<%= FormatNumber(sShipping,2) %></TD></TR>
<TR><TD COLSPAN=4 ALIGN="Right"><B>Total:</B></TD><TD ALIGN="Right">$<%= FormatNumber(sTotal,2) %></TD></TR>
</TABLE>
<%
End Sub

' We implemented this this way so if you attach it to a database you'd only need one call per item
Function GetItemParameters(iItemID)
Dim aParameters ' Will contain 3 string values : image path, description, price
' However we need to keep price so it can be converted to a
' single for computation hence no currency symbol. This array
' can also be expanded to contain any other information about the
' product that you might want to pull from the DB.
Select Case iItemID
Case 1
aParameters = Array("Item 1", "ASP 101 T-Shirt", "15.00")
Case 2
aParameters = Array("Item 2", "ASP 101 Kite", "17.50")
Case 3
aParameters = Array(".Item 3", "ASP 101 Watch", "35.00")
Case 4 ' Not in use because we couldn't draw a pen in a few seconds!
aParameters = Array("Item 4", "ASP 101 Pen", "5.00")
End Select
' Return array containing product info.
GetItemParameters = aParameters
End Function
%>

<% ' ***** Begin the infamous runtime script *****
' Declare our Vars
Dim dictCart ' as dictionary
Dim sAction ' as string
Dim iItemID ' as integer
Dim iItemCount ' as integer

' Get a reference to the cart if it exists otherwise create it
If IsObject(Session("cart")) Then
Set dictCart = Session("cart")
Else
' We use a dictionary so we can name our keys to correspond to our
' item numbers and then use their value to hold the quantity. An
' array would also work, but would be a little more complex and
' probably not as easy for readers to follow.
Set dictCart = Server.CreateObject("Scripting.Dictionary")
End If

' Get all the parameters passed to the script
sAction = CStr(Request.QueryString("action"))
iItemID = CInt(Request.QueryString("item"))
iItemCount = CInt(Request.QueryString("count"))
%>
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>
<TR><TD>
<%
' Select action based on user input
Select Case sAction
Case "add"
AddItemToCart iItemID, iItemCount
ShowItemsInCart
%>
</TD></TR>
<TR><TD ALIGN="right">
<A HREF="./shopping.asp?action="><IMG SRC="./cart/checkout.gif" BORDER=0 WIDTH=46 HEIGHT=46 ALT="Looking Looking"></A>
<A HREF="./shopping.asp?action=checkout"><IMG SRC="./cart/checkout.gif" BORDER=0 WIDTH=46 HEIGHT=46 ALT="Checkout"></A><BR>
<%
Case "del"
RemoveItemFromCart iItemID, iItemCount
ShowItemsInCart
%>
</TD></TR>
<TR><TD ALIGN="right">
'<A HREF="./shopping.asp?action="><IMG SRC="./cart/checkout.gif" BORDER=0 WIDTH=46 HEIGHT=46 ALT="Continue Looking"></A>
<A HREF="./shopping.asp?action=checkout"><IMG SRC="./cart/checkout.gif" BORDER=0 WIDTH=46 HEIGHT=46 ALT="Checkout"></A><BR>
<%
Case "viewcart"
ShowItemsInCart
%>
</TD></TR>
<TR><TD ALIGN="right">
<A HREF="./shopping.asp?action="><IMG SRC="./cart/checkout.gif" BORDER=0 WIDTH=46 HEIGHT=46 ALT="Continue Looking"></A>
<A HREF="./shopping.asp?action=checkout"><IMG SRC="./cart/checkout.gif" BORDER=0 WIDTH=46 HEIGHT=46 ALT="Checkout"></A><BR>
<%
Case "checkout"
PlaceOrder
%>
</TD></TR>
<TR><TD ALIGN="left">
<BR>
<H2>Thank you for your order!</H2>

<%
Case Else ' Shop
ShowFullCatalog
%>
</TD></TR>
<TR><TD ALIGN="right">
<A HREF="./shopping.asp?action=viewcart"><IMG SRC="./cart/viewcart.gif" BORDER=0 WIDTH=46 HEIGHT=46 ALT="View Cart Contents"></A>
<%
End Select

' Return cart to Session for storage
Set Session("cart") = dictCart
%>
</TD></TR>
</TABLE>

 
Chech this code here.
FAQ333-4911

________
George, M
Searches(faq333-4906),Carts(faq333-4911)
 
I have changed a bit to make that code to work.

________
George, M
Searches(faq333-4906),Carts(faq333-4911)
 
thanks for that george....but im getting an error:

Error Type:
ADODB.Connection (0x800A0E78)
Operation is not allowed when the object is closed.
/newcart.asp, line 56

thats in relation to :
if Session("cart").Count>0 then

do youve ne idea why im gettin this?


 
ok i no a reckin yer heads at this stage but cud some1 please have a look at this code and c where im goin wrong!!!
problem is that i keep getting "ur cart is empty"...

il be forever gratefull!

page 2 where details of flight are returned and quantity must be entered!!!

<%

'Dimension variables
Dim connection 'Holds the Database Connection Object
Dim check 'Holds the recordset for the records in the database
Dim data 'Holds the SQL query for the database
Dim hols
Dim flight_id


Set connection = Server.CreateObject("ADODB.Connection")

Call connection.Open ("auction1")

Set data = Server.CreateObject("ADODB.Recordset")

check = "SELECT * FROM Holidays WHERE Destination = '"_
& CStr (Request ( "places")) & "'" _

& " AND Leaving = '"_
& CStr (Request ( "from")) & "'" _


& " AND arrTime = '"_
& CStr (Request ( "arr")) & "'"_

& " AND depTime = '"_
& CStr (Request ( "dep")) & "'"

data.Open check, connection

%>
<TH> Price of Flight:</TH>
<%Response.Write (data("Price")) %><br><br>
<TH> Number of Flight:</TH>
<% Response.Write (data("flightID"))%><br><br>

<%flight_id= data("flightID")%>
<%hols = hols & "|" & data("Price") & "|" & data("flightID")
Session ("cart") = hols%>

<STRONG> <FONT SIZE ="3">Quantity:&nbsp</FONT></STRONG>
<INPUT TYPE = "text" SIZE ="3"
NAME = "qty" VALUE = "1")>

<STRONG> <FONT SIZE ="3">Add:&nbsp</FONT></STRONG>
<INPUT TYPE = "text" SIZE ="3"
NAME = "add" VALUE = "<%=data("flightID")%>"></TD>

<STRONG> <FONT SIZE ="3">Delete:&nbsp</FONT></STRONG>
<INPUT TYPE = "text" SIZE ="3"
NAME = "del" VALUE = ""></TD>

</CENTER>
</FORM>
<BR> <BR> <BR>
<CENTER><A HREF = "newcart.asp">ACCEPT</A></CENTER>



this is the code i received from george and to add and delete quantity from cart!!!!

<%

Dim cart_no
Dim cart
Dim add
Dim qty
Dim del
Dim recDate
Dim Count
Dim i
Dim cartItems
Dim items
Dim Price

if not isObject(Session("cart")) then
set Session("cart")=Server.CreateObject("Scripting.Dictionary")
end if

add=Request("add")
qty=Request("qty")
del=Request("del")

if add<>"" then
'check if data is no there
if not Session("cart").Exists(add) then 'add to cart
'get data from database this example is without
set recData=Server.CreateObject("Scripting.Dictionary")
recData.Add "cart_no",add
recData.Add "qty",1
recData.Add "Price",100
'store the cart to it's cart_no
Session("cart").Add add,recData
else 'flight_id exists
'add only the qty
Session("cart")(add)("qty")=Session("cart")(add)("qty")+CInt(qty)
end if
end if
if del<>"" then
'remove from cart
Session("cart").Remove(del)
end if
'store back to session object


'here display your cart
if IsObject(Session("cart")) then
if Session("cart").Count>0 then
cartItems=Session("cart").Items
For i=Lbound(cartItems) to Ubound(cartItems)
cart_no=cartItems(i)("cart_no")
Price=cartItems(i)("Price")
qty=cartItems(i)("qty")

Response.Write (i+1)&": "&cart_no&" - "&Price&" - "& qty &"<br>"
Next
else
Response.Write ("ur cart is empty")
end if
end if
%>


 
Does my code works?


________
George, M
Searches(faq333-4906),Carts(faq333-4911)
 
yes it works to the extent that no matter what quantity i enter i keep getting "ur cart is empty"!!

 
Then somehow you cant use "Scripting.Dictionary" object.
Try this simple code to test it.
Code:
set recData=Server.CreateObject("Scripting.Dictionary")
recData.Add "test","test value"
response.write recData("test")

If this doesnt work i'll make one using a database.

________
George, M
Searches(faq333-4906),Carts(faq333-4911)
 
thanks for getting back to me.

no george, i tried again but im still getting "ur cart is empty"!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top