Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access_asp thread: Connecting access to shopping cart code


Message #1 by "Scott Fagner" <pmsf42@m...> on Fri, 13 Dec 2002 07:23:57
I have been working for days trying to connect my database to a shopping 
cart code.  I have tried to do it with my limited knowledge and research 
and am now at the point of programing suicide. The code I am showing is a 
bit long and I hope someone can figure it out.  I am also having problems 
using the database to point to a image file in a image folder, I have a 
Field that designates the file name so I can match it to the image in the 
image folder.

Access Fields: ProdNumber, ProdType, ProdName, ProdFormat(Image.jpg in
               Image folder), ProdRetail, ProdSale
Access File Name: Inventory.mdb (listed in the include file)
Access Table Name: RetailProducts
Include File: Maps database path - name: strInventory

The Form on the previous page is a drop down list refrencing the ProdType 
in the database ie. keybords, printers, CDRoms etc.

What I want to do is to have the shopping cart list everything by ProdType 
for adding to cart.  

This was a simple cart code I downloaded from ASP101, the code that 
follows is taken out of the middle of the code where I believe that my 
coding is to be inserted.  The comments are included.

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 = 3
	%>
	
<TABLE Border=1 CellPadding=3 CellSpacing=1 width="294" align="center">
  <TR bgcolor="#000000"> 
    <TD><b><font color="#FFFFFF">Image</font></b></TD>
    <TD><b><font color="#FFFFFF">Product</font></b></TD>
    <TD><b><font color="#FFFFFF">Price</font></b></TD>
    <TD><b><font color="#FFFFFF">Add Item To Cart</font></b></TD>
  </TR>
  <%
	For I = 1 to iItemCount
		aParameters = GetItemParameters(I)
		%>
  <TR> 
    <TD> 
      <div align="center"><IMG SRC="<%= aParameters(0) %>"></div>
    </TD>
    <TD><%= aParameters(1) %> 
      <div align="center"></div>
    </TD>
    <TD> 
      <div align="center">$<%= aParameters(2) %></div>
    </TD>
    <TD> 
      <div align="center"><A HREF="./shopping.asp?action=add&item=<%= I %
>&count=1">Add 
        this to my cart!</A></div>
    </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 align="center">
  <TR bgcolor="#000000"> 
    <TD width="39"> 
      <div align="center"><b><font color="#FFFFFF">Image</font></b></div>
    </TD>
    <TD width="69"> 
      <div align="center"><b><font color="#FFFFFF">Product</font></b></div>
    </TD>
    <TD width="59"> 
      <div align="center"><b><font 
color="#FFFFFF">Quantity</font></b></div>
    </TD>
    <TD width="31"> 
      <div align="center"><b><font color="#FFFFFF">Price</font></b></div>
    </TD>
    <TD width="37"> 
      <div align="center"><b><font color="#FFFFFF">Totals</font></b></div>
    </TD>
  </TR>
  <%
	sTotal = 0
	For Each Key in dictCart
		aParameters = GetItemParameters(Key)
		%>
  <TR> 
    <TD ALIGN="Center" width="39"><%= Key %> 
      <div align="center"></div>
    </TD>
    <TD ALIGN="Left" width="69"><%= aParameters(1) %> 
      <div align="center"></div>
    </TD>
    <TD ALIGN="Center" width="59"><%= dictCart(Key) %> 
      <div align="center"></div>
    </TD>
    <TD ALIGN="Right" width="31"> 
      <div align="center">$<%= aParameters(2) %></div>
    </TD>
    <TD ALIGN="Right" width="37"> 
      <div align="center">$<%= FormatNumber(dictCart(Key) * CSng
(aParameters(2)),2) %></div>
    </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" width="37">$<%= FormatNumber(sShipping,2) %></TD>
  </TR>
  <TR> 
    <TD COLSPAN=4 ALIGN="Right"><B>Total:</B></TD>
    <TD ALIGN="Right" width="37">$<%= 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
("./images/shop_shirt.gif", "ASP 101 T-Shirt", "15.00")
		Case 2
			aParameters = Array("./images/shop_kite.gif", "ASP 
101 Kite", "17.50")
		Case 3
			aParameters = Array
("./images/shop_watch.gif", "ASP 101 Watch", "35.00")
		Case 4 ' Not in use because we couldn't draw a pen in a 
few seconds!
			aParameters = Array("./images/shop_pen.gif", "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=">ContinueShopping 
<IMG SRC="./images/shop_look.gif" BORDER=0 WIDTH=46 HEIGHT=46 
ALT="Continue Looking"></A> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp<A HREF="./shopping.asp?
action=checkout">Check Out <IMG SRC="./images/shop_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=">ContinueShopping 
<IMG SRC="./images/shop_look.gif" BORDER=0 WIDTH=46 HEIGHT=46 
ALT="Continue Looking"></A> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp<a href="./shopping.asp?
action=checkout">Check 
      Out </a><A HREF="./shopping.asp?action=checkout"><IMG 
SRC="./images/shop_checkout.gif" BORDER=0 WIDTH=46 HEIGHT=46 
ALT="Checkout"></A><BR>

I am not interested in anything other that getting my database to work and 
show the images.
Thanks for any help you can give me.
Message #2 by "Ken Schaefer" <ken@a...> on Fri, 13 Dec 2002 18:24:33 +1100
Scott,

You've just posted a hundred lines of code, but you haven't explained what
the problem is that you're facing.

You can't connect to the database? You can't get the data you want? It's not
displaying the way you want it to? You're getting an error?

What, exactly, is the problem?

Cheers
Ken

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: "Scott Fagner" <pmsf42@m...>
Subject: [access_asp] Connecting access to shopping cart code


: I have been working for days trying to connect my database to a shopping
: cart code.  I have tried to do it with my limited knowledge and research
: and am now at the point of programing suicide. The code I am showing is a
: bit long and I hope someone can figure it out.  I am also having problems
: using the database to point to a image file in a image folder, I have a
: Field that designates the file name so I can match it to the image in the
: image folder.
:
: Access Fields: ProdNumber, ProdType, ProdName, ProdFormat(Image.jpg in
:                Image folder), ProdRetail, ProdSale
: Access File Name: Inventory.mdb (listed in the include file)
: Access Table Name: RetailProducts
: Include File: Maps database path - name: strInventory
:
: The Form on the previous page is a drop down list refrencing the ProdType
: in the database ie. keybords, printers, CDRoms etc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Message #3 by "Scott Fagner" <pmsf42@m...> on Fri, 13 Dec 2002 08:27:10
Thanks for responding, I am in begining asp class and I may have bitten 
off more than I can chew. This is the first time I have posted any 
messages for any help - normally I just keep trying until I understand the 
process.  The code that I posted was from ASP101 and seemed pretty simple 
at first and it probably is, I am just not comprehending the way to set up 
my database into this code - and I don't ususaly ask for assistance 
because at this early point in my coding experience I'm not always sure 
how to even ask a question.  My problem is that my logic in solving this 
situation is a bit jarbled and I am sure that I can't see the forest for 
the trees.  
I am having difficulty relating the collecting form page to the action 
page because I am using a drop down list that has a listing of a single 
field in the database.  I also need to call other fields from the database 
and display them in some kind of multiple listing loop based on the 
category of products in the database.
I guess that the problem is not that I can't connect to the database but 
more that I don't know what process or sequence to use in order to show 
multiple items in this shopping cart.
The format is as follows:

    Image         ProductName        Price    SalePrice

   Image.gif     XXX-614 Printer    $00.00     $0.00    Add    Remove
   Image2.jpg    Z14-X01 Printer    $00.00     $0.00    Add    Remove
                                                                      S&H
                                                                      Tax
                                                                      Total
I have tried multiple ways to code it but I posted the original
code that I was working from.  I may be asking too much of your
time to figue this out for me so let me know and I will not post
so much of a problem next time.
Thanks, Scott




> Scott,

You've just posted a hundred lines of code, but you haven't explained what
the problem is that you're facing.

You can't connect to the database? You can't get the data you want? It's 
not
displaying the way you want it to? You're getting an error?

What, exactly, is the problem?

Cheers
Ken

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: "Scott Fagner" <pmsf42@m...>
Subject: [access_asp] Connecting access to shopping cart code


: I have been working for days trying to connect my database to a shopping
: cart code.  I have tried to do it with my limited knowledge and research
: and am now at the point of programing suicide. The code I am showing is a
: bit long and I hope someone can figure it out.  I am also having problems
: using the database to point to a image file in a image folder, I have a
: Field that designates the file name so I can match it to the image in the
: image folder.
:
: Access Fields: ProdNumber, ProdType, ProdName, ProdFormat(Image.jpg in
:                Image folder), ProdRetail, ProdSale
: Access File Name: Inventory.mdb (listed in the include file)
: Access Table Name: RetailProducts
: Include File: Maps database path - name: strInventory
:
: The Form on the previous page is a drop down list refrencing the ProdType
: in the database ie. keybords, printers, CDRoms etc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


  Return to Index