Print Page | Close Window

asp cart prob

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=15892
Printed Date: 30 March 2026 at 3:26am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: asp cart prob
Posted By: ainsworth14
Subject: asp cart prob
Date Posted: 19 July 2005 at 4:18pm

hi all

i have built this cart using a well known asp help book, but i am having trouble changing a few things about to customise it more.
 
Could some please tell me why i need the '2' in the following:
 
FOR i = 0 TO UBOUND (localCart, 2)
 
is it purely because it is the second time i have referenced it in the code?
 
also i want to pass hidden fields for each column in my array (id, artist, title etc) it works as it is but if i leave the line as
 
FOR tq = 0 TO UBOUND( localCart, 2 )
 
i and up sending across about 50 lots of empty hidden fields
and if i change to
 
FOR tq = 0 TO UBOUND( localCart, 1 )
 
i send about 5 lots of hidden fields, obviousdly this is better but all i want is to send across only the amount that is in the shaopping basket?!
 
can anyone help - code is below, sorry for the lengh, i have tried to cut out any useless html for this post.
 
thanks
mark
 
 
<%

'define constants
CONST CARTPID = 0
CONST CARTPARTIST = 1
CONST CARTPPRICE = 2
CONST CARTPQUANTITY = 3
CONST CARTPTITLE = 4
CONST CARTPFORMAT = 5

'get the shopping cart
IF NOT isArray (Session("cart")) THEN
DIM localCart(5,20)
ELSE
localCart = Session("cart")
END IF


'get product information
productID = TRIM(request("pid"))
catID = TRIM(request("catID"))
artist = TRIM(request("artist"))
title = TRIM(request("title"))
format = TRIM(request("format"))
price = TRIM(request("price"))

'add to cart
IF productID <> "" THEN
foundIT = FALSE
FOR i = 0 TO UBOUND (localCart, 2)
IF localCart( CARTPID, i ) = productID THEN
localCart ( CARTPQUANTITY, i ) = localCart (CARTPQUANTITY, i) + 1
foundIT = TRUE
EXIT FOR
END IF
NEXT
IF NOT foundIT THEN
FOR i = 0 TO UBOUND (localCart, 2)
If localCart(CARTPID, i ) = "" THEN
localCart (CARTPID, i) = productID
localCart (CARTPARTIST, i) = artist
localCart (CARTPTITLE, i) = title
localCart (CARTPFORMAT, i) = format
localCart (CARTPPRICE, i) = price
localCart (CARTPQUANTITY, i) = 1
EXIT FOR
END IF
NEXT
END IF
END IF

' Update Shopping Cart Quantities
IF Request( "updateQ" ) <> "" THEN
  FOR i = 0 TO UBOUND( localCart, 2 )
    newQ = TRIM( Request( "pq" & localCart( CARTPID, i ) ) )
 deleteProduct = TRIM(Request("pd" & localCart (CARTPID,i)))
 IF newQ = "" or newQ = "0" or deleteProduct <> "" THEN
      localCart( CARTPID, i ) = ""
 ELSE
      IF isNumeric( newQ ) THEN
        localCart( CARTPQUANTITY, i ) = newQ
   END IF
 END IF
  NEXT
END IF


'update Session variable with array
Session ("cart") = localCart


%>

<%
FOR i = 0 TO UBOUND( localCart, 2 )
IF localCart( CARTPID, i ) <> "" THEN
orderTotal = orderTotal + ( localCart( CARTPPRICE, i ) * localCart( CARTPQUANTITY, i ) )
%>
<TR></TR>
<TD align="left" class="bottom_border">
<span class="page-nav-strong"><%=emptycart_msg%><%=Server.HTMLEncode(localCart(CARTPARTIST, i))%></span>
</TD>
<TD align="left" class="bottom_border">
<span class="page-nav-strong"><%=Server.HTMLEncode(localCart(CARTPTITLE, i))%></span>
</TD>
<TD align="left" class="bottom_border">
<span class="page-nav-strong">£<%=FormatNumber(localCart(CARTPPRICE, i))%></span>
</TD>
<TD align="left" class="bottom_border">
<input name="pq<%=localCart(CARTPID, i )%>" type="text" size="4" value="<%=localCart(CARTPQUANTITY, i )%>">
</td>
<TD align="left" class="bottom_border">
<input name="pd<%=localCart(CARTPID, i )%>" type="checkbox" value="1">
<span class="page-nav-strong">Delete Item</span>
<BR><div align="right"><a href="productpage.asp?category=<%=catID%>&productdetail=<%=localCart( CARTPID, i )%>" class="recent_searches">view item....</a></div>
</td>
</TR>
<%
END IF
NEXT
%>
<TR bgcolor="#F4F7FD">
<TD colspan="2" align="right" class="td-top-bottom_border">
<span class="activeCategory"><B>Total:</B></span>
</TD>
<TD align="left" colspan="2" class="td-top-bottom_border">
<span class="page-nav-strong">£<%=FormatNumber(orderTotal)%> + shipping</span>
</TD>
<TD align="left" bgcolor="#ffffff" class="td-top-bottom_border">
<input type="image" img src="assets/images/updatecart.gif">
</form></TD>
</TR>
<TR>
<TD colspan="5"><hr color="#F4F7FD"></td>
</tr>
<TR class="row-top-bottom_border">
<TD align="center">
<a href="default.asp" class="main_nav"><< previous page</a>
</TD>
<td>&nbsp;

</td>
<td>&nbsp;</td>
<%
totalquantity = 0
%>
<%
FOR tq = 0 TO UBOUND( localCart, 2 )
IF localCart( CARTPPID, tq ) <> "" THEN
totalquantity = totalquantity + localCart( CARTPQUANTITY, tq)
END IF
NEXT
%>

<td align="center">
<a href="clearcart.asp" class="main_nav">empty cart</a></td>
<TD>
  <div align="center">
 
  <FORM METHOD="post" action="checkout.asp" style="margin-bottom:0;">
  <input type="image" img src="assets/images/checkout.gif">
   <%FOR i = 0 TO UBOUND( localCart, 1)%>
  <input type="hidden" name="idlist" value="<%=Server.HTMLEncode(localCart(CARTPID, i))%>">
  <input type="hidden" name="artistlist" value="<%=Server.HTMLEncode(localCart(CARTPARTIST, i))%>">
  <input type="hidden" name="titlelist" value="<%=Server.HTMLEncode(localCart(CARTPTITLE, i))%>">
  <input type="hidden" name="formatlist" value="<%=Server.HTMLEncode(localCart(CARTPFORMAT, i))%>">
  <input type="hidden" name="quantity" value="<%=Server.HTMLEncode(localCart(CARTPQUANTITY, i))%>">
  <%NEXT%>
  <input type="hidden" name="ordertotal" value="<%=orderTotal%>">
  <input type="hidden" name="totalquantity" value="<%=totalquantity%>">
    </div></td>
  </tr>
  <tr>
  <td bgcolor="#F4F7FD" valign="top" class="td-top-bottom_border" colspan="5"><span class="activeCategory">You may also like:</span></td></tr>
<%FOR a = 1 TO y%>
  <tr>
  <td bgcolor="#F4F7FD" valign="top" class="td-top-bottom_border" colspan="1"><%=recommend_image(a)%></td>
  <td bgcolor="#F4F7FD" class="td-top-bottom_border" colspan="4"><a href="productpage.asp?category=<%=recommend_catid(a)%>&productdetail=<%=recommend_id(a) %>" class="category1"><%=recommend_artist(a)%>  - 
      <span class="category2"><%=recommend_title(a)%></span></a><P>
   <span class="category3"><%=recommend_description(a)%>...</span>[<a href="productpage.asp?category=<%=r




Replies:
Posted By: dpyers
Date Posted: 19 July 2005 at 9:57pm
localCart(5,20) indicates it's a multi dimensional array - think of it as 5 rows with 20 columns in each row.

UBOUND( localCart, 2 )
gives you the total number of columns in the second dimension (20 columns x 5 rows) of the array.

UBOUND( localCart, 1 )
gives you the total number of rows in the first dimension (5 rows) of the array. This is the default for Ubound and you don't have to specify ",1"

You're seeing blanks because some elements of the array are not populated or have unprintable characters in them.

EDIT:
Remember when working with arrays, counting starts at 0, not at 1 so in the statement:
x = Ubound(localCart)
x will contain a value of 4, not of 5.



-------------

Lead me not into temptation... I know the short cut, follow me.


Posted By: ainsworth14
Date Posted: 20 July 2005 at 5:28pm
okay, thanks a lot - i think i am getting to grips with,
 
as for the empty values (not populated) will these always be there?


Posted By: dpyers
Date Posted: 20 July 2005 at 6:34pm
It's the DIM localCart(5,20) that sets the size/number of fields in the array. Probably something generic for a shopping cart. You could change that to suite your own requirements.

When you check for empty elements in the array, they'll contain a NULL value which is different from a value of "" (empty string).
e.g.
If you set x = "" there's actually invisible end of string characters similar to the invisible line feed/carriage return characters at the end of a line of text.

If you set x = NULL there's no end-of-string characters.

A NULL will sort before an empty string in an ascending sort.
So when checking a database field or an element of an array to see if it's empty a good idea is not to check if it's equal "" or NULL but to check if it's greater than an empty string (If x > "").
That way you don't have to worry about if you're dealing with raw data or with a string.

EDIT: Some people initialize an array before loading it with data by looping through it and setting all the elements to ""
e.g.  - MyArray (index1, index2) = ""


-------------

Lead me not into temptation... I know the short cut, follow me.


Posted By: ainsworth14
Date Posted: 20 July 2005 at 6:44pm
thanks very much - i have managed to get quite a lot done tonight with your guidance!
 
my other main problem with this cart is calculating postage.
 
what i need is someway to have a flat fee for 1 item then to increment the postage by a smaller amount depending on a variable - localcart(CARTPFORMAT,i)
 
like this:
 
first item = 12inch so postage will be £2.50 then the second item is a "2x12inch" so i would want the postage to go up another £1.00 but if another "12inch" was added it will go up by £0.50
 
do you see what i mean?
 
i have tried endless calcualtions but have not fathomed it out, if anyone knows you probably save my life, if not i'll keep trying.
 
thanks for you time
mark


Posted By: dj air
Date Posted: 20 July 2005 at 7:30pm
if items > 1 then

postage = 1.00/number of items

END IF

if postage = 2.50 + Postage

i take it it devides by 2 each time ie 1,.50, 25?

what ammounts does it stop at


Posted By: ainsworth14
Date Posted: 21 July 2005 at 2:18pm
the basic amounts are
 
£2.50 for the first item then £0.50 for each additional item BUT
the additional items could be a different format (2x12inch) so then i want the additional charge to be £1.00 instead of £0.50
and so on
 
is this clear? i really appreciate your help
thanks
mark


Posted By: dj air
Date Posted: 22 July 2005 at 7:50am
ok

postage = 2.50

if item = "12 inch" then

strPostageextra = 1.00

elseif item = "other item"

strPostage = "0.50"

END IF

postage = postage + (itemCount -1 *strPostageextra)

hope that gives an idea, if its a stict cart then you can do it code wise using elseif

i am working on a cart system, but not had time lately to work on it Cry



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net