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!

problem with shopping cart!!

Status
Not open for further replies.

tuam1234

Technical User
Feb 6, 2004
52
IE

PLEASE PLEASE HELP.. ANY HELP WILL BE GREATFULLY APPRECIATED!!

this is the code for view.asp.
when i click the button on the page that allows the user to view the contents of the shopping cart i get this error:

Error Type:
Microsoft VBScript runtime (0x800A0009)
Subscript out of range: '[number: 3]'
/viewcart.asp, line 93

this is a snippet, from the code below, where this error occurs.

<%
subtotal = CCur (splitArray (3))*_
CInt(quantity)
total = total + subtotal
%>
<TD>
<%=subtotal%>
</TD>
</TR>




<% @LANGUAGE = VBSCRIPT %>
<% Option Explicit %>

<%'checklogin.asp %>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional //EN">
<HTML>

<HEAD>
<TITLE> View</TITLE>
<link rel="stylesheet" href="../../../../xml%20fligts/style.css" type="text/css">
</HEAD>



<BODY BGCOLOR = "#FFFF99" class="stylesheet1">
<CENTER>

<H1><U>High Flyers </U></H1><BR><BR>
<H2>The total price for your choosen flight is: </H2>



<%
'Dimension variables
Dim i 'Holds the Database Connection Object
Dim splitArray 'Holds the recordset for the records in the database
Dim quantity 'Holds the SQL query for the database
Dim total
Dim j
Dim subtotal
Dim bought

If Request.Cookies("Books")="" Then %>
<CENTER>
<H3>
<FONT COLOR = "red">
Your cart is empty.
</FONT>
</H3>
</CENTER>

<%
Else%>
<TABLE BORDER = "1" ALIGN = "CENTER" WIDTH = "100%"
CELLPADDING = "0" CELLSPACING = "3">

<CAPTION>
<H2>
<FONT COLOR = "brown">
This is your current cart.
</FONT>
</H2>
</CAPTION>
<COLGROUP>
<COLSPAN = =="6" ALIGN = "CENTER">
</COLGROUP>

<TR>
<TH><U>Title</U></TH>
<TH><U>Author</U></TH>
<TH><U>ISBN</U></TH>
<TH><U>PRICE</U></TH>
<TH><U>QUANTITY</U></TH>
<TH><U>SUBTOTAL</U></TH>
</TR>

<% total = 0



For i= 1 To Request.Cookies("Books").Count/2

splitArray = Split( Request.Cookies("Books")_
("book" & CStr (i)),"|" )
%> <TR>

<%
For j = 0 To UBound (splitArray) %>
<TD> <% =splitArray(j) %> </TD>
<%
Next

quantity = Request.Cookies("Books")("quantity" & _
CStr(i))
%>

<TD>
<%=quantity%>
</TD>

<%
subtotal = CCur (splitArray (3))*_
CInt(quantity)

total = total + subtotal
%>
<TD>
<%=subtotal%>
</TD>

</TR>
<% Next
%>
<TR>
<TH COLSPAN = "6" ALIGN= "right"><U>Total:</U></TH>
<TD> <%=total%></TD>
</TR>

</TABLE>
<% End If %>
<br>

<CENTER><A HREF = "buyit.asp">Back To Shopping</A>
</CENTER>


</BODY>
</HTML>








 
Check the Ubound of the splitArray.

________
George, M
faq333-4906
 
thanks for replying.
sorry, but what exactly does UBound mean??
 
This code gives you the error
splitArray (3)

Response.Write Ubound(splitArray) should display at least 3 or the array index does not exist and gives you the error.

________
George, M
faq333-4906
 
when i took out :

<%
'subtotal = CCur (splitArray (3))*_
' CInt(quantity)

'total = total + subtotal
%>

and wrote in:
Response.Write UBound(splitArray)

i got no error but got "3 -1 -1" wrote beside my cart contents table.

what does this mean? please help if uv any idea!
 
Yep you have an loop there first time it displays 3 which means it's corect but second time there is no splitArray varable.

________
George, M
faq333-4906
 
not looking to be spoon fed but why is it that i dont have a splitArray the 2nd time?? could u make any suggestion to solve this problem!!

thanks for gettn back to me.
 
Try to display those cookies values and see if it's ok

For i= 1 To Request.Cookies("Books").Count/2

Response.Write Request.Cookies("Books")("book" & CStr (i))
Next

________
George, M
faq333-4906
 
i added in those lines of code and got "1|1|1|33" which is the information that is displayed in the cart table thats displayed on the view page!!!
 
Your problem could be generated by the Request.Cookies("Books").Count/2, this number could be rounded to an upper value other then the lower one.

I still dont knw why you are using thi Request.Cookies("Books").Count/2? What this does?


________
George, M
faq333-4906
 
to be completly honest, i got the code from a deitel book and ran it exactly as it was but got the error.

Error Type:
Microsoft VBScript runtime (0x800A0009)
Subscript out of range: '[number: 3]'
/viewcart.asp, line 93

i ant total sure what its ment to do either but when i remove it, leaving i=1 then i dont have a problem with the error above but odviously the cart isnt correctly displayed then.

id appreciate any further suggestion!!

but if iv recked your head at this stage, then thanks a million for sticking with and replying..



 
Ah, well i knw that most books has errors in code, not shure if intended but this could be the problem.

Better way would be if you explain us what kind of cart you want to make and maybe we will help.

________
George, M
faq333-4906
 
basically, im creating an online flight booking system. i just want the client to be able to enter the quantity of flights that they wish to buy, multiply this by the price of the flight,then create total cost. my aim in creating the cart is that the client will be able to go back and buy other flights and then add these to the subtotal, creating an updated subtotal... does this moke sense??

do you suggest any other way of achieving this, other than what i was trying to at the min???
 
Using Session object this could look like this, asuming that you would use a flight_id for the flights
Code:
cart.asp
<%
if Session("cart")<>"" then Session("cart")=Server.CreateObject("Scripting.Dictionary")

add=Request("add")
qty=Request("qty")
del=Request("del")
set cart=Session("cart")
if add<>"" then
 'check if data is no there
 if not cart.Exists(add) then  'add to cart
 'get data from database
  sql="select * from flights where flight_id="&add
  rs.Open sql,connObj,3,3
   set recData=Server.CreateObject("Scripting.Dictionary")
   recData.Add "flight_id",rs("flight_id")
   recData.Add "qty",1
   recData.Add "price",rs("price")
  'store the cart to it's flight_id
  cart.Add add,recData
  rs.Close
 else 'flight_id exists
  'add only the qty
  cart(add)("qty")=cart(add)("qty")+CInt(qty)
 end if
end if
if del<>"" then
 'remove from cart
 cart.Remove(del)
end if
...

'here display your cart
for i=0 to cart.Count-1
 flight_id=cart(i)("flight_id")
 price=cart(i)("price")
 qty=cart(i)("qty")
 'display those values in an table
next
%>

How to use

cart.asp?add=101 - will add this rec id into cart
cart.asp?add=101&qty=2 - will add 2 to qty of the 101
cart.asp?del=101 - will remove this cart

I also posted this code into faq333-4911


________
George, M
faq333-4906
 
thanks for that. il give it ago tonight and see how i get on.. il keep ya posted :>
 
iv changed the code to suit my code and im gettin the error:

Error Type:
Microsoft VBScript runtime (0x800A01A8)
Object required: ''
/newcart.asp, line 57

error occurs at this line:
For i=0 to cart.Count-1


can anyone suggest why im gettin this error!

thankful for all replies!

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<%

Dim cart
Dim add
Dim qty
Dim del
Dim recDate
Dim Count
Dim i

if Session("cart")<>"" then Session("cart")=Server.CreateObject("ADODB.Connection")

add=Request("add")
qty=Request("qty")
del=Request("del")
cart=Session("cart")
if add<>"" then
'check if data is no there
if not cart.Exists(add) then 'add to cart
'get data from database
sql="select * from flights where flight_id="&add
rs.Open sql,connObj,3,3
set recData=Server.CreateObject("Scripting.Dictionary")
recData.Add "flight_id",rs("flight_id")
recData.Add "qty",1
recData.Add "price",rs("price")
'store the cart to it's flight_id
cart.Add add,recData
rs.Close
else 'flight_id exists
'add only the qty
cart(add)("qty")=cart(add)("qty")+CInt(qty)
end if
end if
if del<>"" then
'remove from cart
cart.Remove(del)
end if
'store back to session object
Session("cart")=cart

'If Request ("qty") <> "" Then
'Response.Write ("ur cart is empty")
'Else

'here display your cart
For i=0 to cart.Count-1
flight_id=cart(i)("flight_id")
price=cart(i)("price")
qty=cart(i)("qty")
'display those values in an table
Response.Write ("flight_id")
Response.Write ("price")
Response.Write ("qty")
'Exit For
'End If
Next
%>


</body>
</html>
 
I've got time to make an working example without database
Use this code do the test

cart.asp?add=101 - will add this rec id into cart
cart.asp?add=101&qty=2 - will add 2 to qty of the 101
cart.asp?del=101 - will remove this cart

Code:
<%
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
%>


________
George, M
Searches(faq333-4906),Carts(faq333-4911)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top