Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Location
    Sumter, SC
    Posts
    178
    Thanks
    10
    Thanked 4 Times in 4 Posts

    Question Beginner in over my head

    Hey everyone,
    I'm new here, and somewhat new to ASP and Database driven websites. I'm working on a project for one of my classes, but the site requires a "shopping cart" of sorts. I'm working with Dreamweaver 8 and MS Access 2003 on IIS 5.1. I'm having trouble getting the data flow logic worked out for how and when the information needs to enter the database. I need to be able to allow the user to choose tee shirt size and color. From these selections the Item Number is picked dynamically (as each color has its own item number). Then the user presses the "Add to Cart" button, gets redirected to the order page that should display the added item and a text feild where they can enter the desired quanity. The user then has the option to continue shopping or check out. I have figured out how to work with session variables to pass around form data from one page to another. The problem starts when I have more than one item I need to display in the cart. Along with this issue, I'm also not sure when I should actually enter the order information inot the database itself. I can provide what ever code you need to help me out. I'm really in over my head here but I'm the only one on my team with any web design experience at all. I've gone a long way with dreamweavers wizards and things, but now I'm to the point where dreamweaver cant really do it all for me any more. Any help is appreciated. Thanks guys.

  • #2
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,063
    Thanks
    4
    Thanked 8 Times in 8 Posts
    if you are going to use a session variable to hold the cart contents you will need to append the value after each item is added. So let's say you are adding first a tee shirt to the cart you could use something similar to this

    Code:
    Dim itm, clr, qty, sz, prc
    itm = Request.Form("item")
    clr = Request.Form("color")
    qty = Request.Form("quantity")
    sz = Request.Form("size")
    prc = Request.Form("price")
    
    Session("CartContents") = "" 
    Session("CartContents") =  itm & "-" & sz & "-" & clr & "-" & qty & "-" & prc
    That works great for 1 item. But if you have multiple items then you need to make a change. Adding multiple session variables would work if you have a low number of items for sale, but the overhead involved can be enourmous and really slow your site down.

    Now let's say the above user wants to add some jeans to their cart. but if you use the previous code you would overwrite the contents. So we will append the information to the session variable that already exists. You will also need to use something unique to seperate this item from the other items. This will be used later on when you use the split function to make an array of the items. So lets take the previous code and make a change.

    Code:
    Dim itm, clr, qty, sz, prc
    itm = Request.Form("item")
    clr = Request.Form("color")
    qty = Request.Form("quantity")
    sz = Request.Form("size")
    prc = Request.Form("price")
    
    Session("CartContents") = "" 
    If Len(Session("CartContents")) = 0 Then  'there are no items in the cart
    	Session("CartContents") =  itm & "-" & sz & "-" & clr & "-" & qty & "-" & prc 
    Else
    	Session("CartContents") = Session("CartContents") & "|" &  itm & "-" & sz & "-" & clr & "-" & qty & "-" & prc 
    End If

    OK so now we have a string with a pipe used to seperate the items. Befor ethey are done shopping the user wants to add a dvd to their cart. But dvd's don't have a size or color. So do we just add the item, quantity and price? the answer is no. You will still add a place for the size and color. What you should do here is place a hidden form element with the value of 0 for the items that don't have size or color so you will use code similar to the last code to add the item.

    Now the user checks out. You need to have a way to break up the Session("CartContents") string. As I mentioned earlier the Split function is used to create an array of items. Then we need to seperate the individual fields so we again use the Split function to create an array. The code to show the contents fo thte cart would look something like this

    Code:
    <table>	
    	<caption>Cart Contents</caption>
    	<tr>
    		<td>Item</td>
    		<td>Size</td>
    		<td>Color</td>
    		<td>Quantity</td>
    		<td>Amount</td>
    	<tr>
    <%
    Dim aCart, i, aItems, x, totalCost
    totalCost = 0
    aCart = Split(Session("CartContents"),"|")
    For i = 0 to UBound(aCart)
    	Response.Write "<tr>"
    	aItems = Split(aCart(i),"-")
    	For x = 0 to UBound(aItems)
    		Response.Write "<td>" & aItems(x) & "</td>"
    		If x = 4 Then
    			totalCost = totalCost + CCur(aItems(4))
    		End If
    	Next
    	Response.Write "</tr>"	
    Next
    %>
    	<tr>
    		<td colspan="4" align="right">Total Cost:</td>
    		<td><%=totalCost%></td>
    	</tr>
    </table>
    that should give you a good head start good luck

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Location
    Sumter, SC
    Posts
    178
    Thanks
    10
    Thanked 4 Times in 4 Posts
    WOW! You make that sound so mundane and simple. Thanks a ton. I assume the table would be part of a form that allows me to have an insert record function upon submit?

  • #4
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,063
    Thanks
    4
    Thanked 8 Times in 8 Posts
    Insert the data into the database when you have the users name, etc and have recieved the payment. At this point you can clear the session variable

  • #5
    Regular Coder
    Join Date
    Mar 2006
    Location
    Sumter, SC
    Posts
    178
    Thanks
    10
    Thanked 4 Times in 4 Posts
    I modified the code you gave me to fit the specifics of what I needed to add into each line, managed to get a text box in there as well for the user to be able to change qty from the default value of 1 if they wanted to. New problems im running into are getting me even more confused as to how to handle them.

    1) For what ever reason the code doesnt act properly. When I use the back button to continue shopping during testing, if I add a new item, it overwrites the existing item. I'm not sure why...

    2) The text box mentioned above is repeating, so how can I get a total value in all of the textboxes? I was thinking along the lines of a for next loop with a concatenated number on the end of the textfield name but not sure if that would work since it would sorta look like this:
    Dim TotalQty
    TotalQty = Document.Order.Quantity.Value
    For x = 0 to UBound(aItems)
    TotalQty += Document.Order.Quantity(x).Value
    Next

    3) Shipping is based on qty; the default shipping cost covers 2 items. Item #3 would cost 2*Default Shipping. I'm not sure how to calculate this since if I simply do TotalQty/2 I could end up with a decimal and I dont need to be multplying by a decimal only the whole number.
    EX: TotalQty = 7
    Shipping = 1.85*('totalqty/2 rounding up to the next whole number')
    Last edited by DakotaChick; 03-23-2006 at 07:46 AM.

  • #6
    Regular Coder
    Join Date
    Mar 2006
    Location
    Sumter, SC
    Posts
    178
    Thanks
    10
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by DakotaChick
    1) For what ever reason the code doesnt act properly. When I use the back button to continue shopping during testing, if I add a new item, it overwrites the existing item. I'm not sure why...
    Figured this one out.
    2) The text box mentioned above is repeating, so how can I get a total value in all of the textboxes? I was thinking along the lines of a for next loop with a concatenated number on the end of the textfield name but not sure if that would work since it would sorta look like this:
    Dim TotalQty
    TotalQty = Document.Order.Quantity.Value
    For x = 0 to UBound(aItems)
    TotalQty += Document.Order.Quantity(x).Value
    Next
    Still trying to get this part to work...i keep getting type mismatch errors....
    3) Shipping is based on qty; the default shipping cost covers 2 items. Item #3 would cost 2*Default Shipping. I'm not sure how to calculate this since if I simply do TotalQty/2 I could end up with a decimal and I dont need to be multplying by a decimal only the whole number.
    EX: TotalQty = 7
    Shipping = 1.85*('totalqty/2 rounding up to the next whole number')
    Figured this out...using qty=(Fix(qty/2)+1)

    Any help with problem #2 would be great...

  • #7
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,063
    Thanks
    4
    Thanked 8 Times in 8 Posts
    are you looking to show the total cost of all orders in a textbox in each row? or do you want to show the subtotal cost of each item in each row with each item's subtotal cost added up at the end with a total cost?

  • #8
    Regular Coder
    Join Date
    Mar 2006
    Location
    Sumter, SC
    Posts
    178
    Thanks
    10
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by miranda
    are you looking to show the total cost of all orders in a textbox in each row? or do you want to show the subtotal cost of each item in each row with each item's subtotal cost added up at the end with a total cost?
    I need to know total quantity that the user enters into the text box. assuming they enter 4 items into the cart, there would be 4 text boxes named Quantity0, Quantity1, etc based on the for loop splitting the contents the first time. The user was 3 of item 1, 2 of item 2, 5 of item 3 and 6 of item 4. I need to know the total of that quantity...

  • #9
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,063
    Thanks
    4
    Thanked 8 Times in 8 Posts
    Declare another variable before you enter the for/next loop. This variable will hold the sum of the items that you want to add up. Now as you are in the loop, just add that value up. if it is the 2nd column that you want to add up then in the for/next loop just add something like this

    total = total + aItems(1)

  • #10
    Regular Coder
    Join Date
    Mar 2006
    Location
    Sumter, SC
    Posts
    178
    Thanks
    10
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by miranda
    Declare another variable before you enter the for/next loop. This variable will hold the sum of the items that you want to add up. Now as you are in the loop, just add that value up. if it is the 2nd column that you want to add up then in the for/next loop just add something like this

    total = total + aItems(1)
    im not really looking to add up the number of text boxes but the value contained in the text boxes. normally this would be rather simple -
    qty = document.[formname].[formfield].value
    qty = qty + document.[formname].[formfield].value
    etc.. but the problem comes in when i dont know how many form fields i'll need to refrence..
    Last edited by DakotaChick; 03-26-2006 at 11:30 PM.

  • #11
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,063
    Thanks
    4
    Thanked 8 Times in 8 Posts
    that is exactly what this code does, i should have followed the previous example I gave and said to use aItems(2) and not aItems(1)

    total = total + aItems(2)

    You have to understand that the array aItems(2) takes the value of the 3rd field. so you aren't adding up the number of textboxes but the value that is inside of it.


    So let's say that you have the following in an 2 arrays 1 for the individual order and another for all of the orders.

    item, color, quantity, size, price

    now lets use the example of 2 tshirts 1 pair of jeans and 3 cd's in the cart
    2 tshirts
    06895324,black,2,m,14.95
    1 pair of jeans
    23156324,blue,1,8,22.95
    3 cd's
    63695658,0,1,0,12.95
    63699563,0,1,0,12.95
    63696875,0,1,0,12.95

    the values held in aItems(2) are 2,1,1,1,1
    so as you iterate through the loop and add them up you will get the following
    total = 0 + 2
    total = 2 + 1
    total = 3 + 1
    total = 4 + 1
    total = 5 + 1

    Since you are already doing the loop on the server it only makes sense to do it there and not on the client which is what document.formname.fieldname.value is. The document object does not exist on the server, it only exists on the client.

    To do this on the client would require a script kinda like this, this is off the top of my head and i haven't tested it.
    Code:
    var total = 0;
    var qty;
    for (i=0;i<document.forms[0].quantity[i];i++){
          qty = "document.forms.quantity"+i+".value"
          total += qty;
    }
    Last edited by miranda; 03-27-2006 at 03:07 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •