home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / ASP / shopping.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  9.0 KB  |  271 lines

  1. <%
  2. '*******************************************************
  3. '*     ASP 101 Sample Code - http://www.asp101.com     *
  4. '*                                                     *
  5. '*   This code is made available as a service to our   *
  6. '*      visitors and is provided strictly for the      *
  7. '*               purpose of illustration.              *
  8. '*                                                     *
  9. '* Please direct all inquiries to webmaster@asp101.com *
  10. '*******************************************************
  11. %>
  12.  
  13. <% ' ***** Begin the functions to be called by the runtime script *****
  14. ' To find the actual runtime code scroll WAY DOWN....
  15.  
  16. ' This function is written to enable the adding of multiples of an item
  17. ' but this sample always just adds one.  If you wish to add different
  18. ' quantities simply replace the value of the Querystring parameter count.
  19. ' We didn't do this because we wanted to keep the whole thing simple and
  20. ' not get into using forms so it stayed relatively readable.
  21. Sub AddItemToCart(iItemID, iItemCount)
  22.      If dictCart.Exists(iItemID) Then
  23.         dictCart(iItemID) = dictCart(iItemID) + iItemCount
  24.     Else
  25.         dictCart.Add iItemID, iItemCount
  26.     End If
  27.     Response.Write iItemCount & " of item # " & iItemID & " have been added to your cart.<BR><BR>" & vbCrLf
  28. End Sub
  29.  
  30. Sub RemoveItemFromCart(iItemID, iItemCount)
  31.      If dictCart.Exists(iItemID) Then
  32.         If dictCart(iItemID) <= iItemCount Then
  33.             dictCart.Remove iItemID
  34.         Else
  35.             dictCart(iItemID) = dictCart(iItemID) - iItemCount
  36.         End If
  37.         Response.Write iItemCount & " of item # " & iItemID & " have been removed from your cart.<BR><BR>" & vbCrLf
  38.     Else
  39.         Response.Write "Couldn't find any of that item your cart.<BR><BR>" & vbCrLf
  40.     End If
  41. End Sub
  42.  
  43. Sub ShowItemsInCart()
  44. Dim Key
  45. Dim aParameters ' as Variant (Array)
  46. Dim sTotal, sShipping
  47.     
  48.     %>
  49.     <TABLE Border=1 CellPadding=3 CellSpacing=1>
  50.         <TR>
  51.             <TD>Item #</TD>
  52.             <TD>Description</TD>
  53.             <TD>Quantity</TD>
  54.             <TD>Remove Item From Cart</TD>
  55.             <TD>Price</TD>
  56.             <TD>Totals</TD>
  57.         </TR>
  58.     <%
  59.     sTotal = 0
  60.     For Each Key in dictCart
  61.         aParameters = GetItemParameters(Key)
  62.         %>
  63.         <TR>
  64.             <TD ALIGN="Center"><%= Key %></TD>
  65.             <TD ALIGN="Left"><%= aParameters(1) %></TD>
  66.             <TD ALIGN="Center"><%= dictCart(Key) %></TD>
  67.             <TD ALIGN="Left"><A HREF="./shopping.asp?action=del&item=<%= Key %>&count=1">Remove One</A>  <A HREF="./shopping.asp?action=del&item=<%= Key %>&count=<%= dictCart(Key) %>">Remove All</A></TD>
  68.             <TD ALIGN="Right">$<%= aParameters(2) %></TD>
  69.             <TD ALIGN="Right">$<%= FormatNumber(dictCart(Key) * CSng(aParameters(2)),2) %></TD>
  70.         </TR>
  71.         <%
  72.         sTotal = sTotal + (dictCart(Key) * CSng(aParameters(2)))
  73.     Next
  74.     
  75.     'Calculate shipping - you might want to pull this out into a function if your shipping 
  76.     ' calculations are more complicated then ours.  ;)
  77.     If sTotal <> 0 Then
  78.         sShipping = 7.5
  79.     Else
  80.         sShipping = 0
  81.     End If
  82.     sTotal = sTotal + sShipping
  83.     %>
  84.     <TR><TD COLSPAN=5 ALIGN="Right"><B>S+H:</B></TD><TD ALIGN="Right">$<%= FormatNumber(sShipping,2) %></TD></TR>
  85.     <TR><TD COLSPAN=5 ALIGN="Right"><B>Total:</B></TD><TD ALIGN="Right">$<%= FormatNumber(sTotal,2) %></TD></TR>
  86.     </TABLE>
  87.     <%
  88. End Sub
  89.  
  90. Sub ShowFullCatalog()
  91. Dim aParameters ' as Variant (Array)
  92. Dim I
  93. Dim iItemCount ' Number of items we sell
  94.     ' If you are really going to use this sample this should probably be pulled from a DB
  95.     iItemCount = 3
  96.     %>
  97.     <TABLE Border=1 CellPadding=3 CellSpacing=1>
  98.         <TR>
  99.             <TD>Image</TD>
  100.             <TD>Description</TD>
  101.             <TD>Price</TD>
  102.             <TD>Add Item To Cart</TD>
  103.         </TR>
  104.     <%
  105.     For I = 1 to iItemCount
  106.         aParameters = GetItemParameters(I)
  107.         %>
  108.         <TR>
  109.             <TD><IMG SRC="<%= aParameters(0) %>"></TD>
  110.             <TD><%= aParameters(1) %></TD>
  111.             <TD>$<%= aParameters(2) %></TD>
  112.             <TD><A HREF="./shopping.asp?action=add&item=<%= I %>&count=1">Add this to my cart!</A></TD>
  113.         </TR>
  114.         <%
  115.     Next 'I
  116.     %>
  117.     </TABLE>
  118.     <%
  119. End Sub
  120.  
  121. Sub PlaceOrder()
  122. Dim Key
  123. Dim aParameters ' as Variant (Array)
  124. Dim sTotal, sShipping
  125.     
  126.     %>
  127.     <TABLE Border=1 CellPadding=3 CellSpacing=1>
  128.         <TR>
  129.             <TD>Item #</TD>
  130.             <TD>Description</TD>
  131.             <TD>Quantity</TD>
  132.             <TD>Price</TD>
  133.             <TD>Totals</TD>
  134.         </TR>
  135.     <%
  136.     sTotal = 0
  137.     For Each Key in dictCart
  138.         aParameters = GetItemParameters(Key)
  139.         %>
  140.         <TR>
  141.             <TD ALIGN="Center"><%= Key %></TD>
  142.             <TD ALIGN="Left"><%= aParameters(1) %></TD>
  143.             <TD ALIGN="Center"><%= dictCart(Key) %></TD>
  144.             <TD ALIGN="Right">$<%= aParameters(2) %></TD>
  145.             <TD ALIGN="Right">$<%= FormatNumber(dictCart(Key) * CSng(aParameters(2)),2) %></TD>
  146.         </TR>
  147.         <%
  148.         sTotal = sTotal + (dictCart(Key) * CSng(aParameters(2)))
  149.     Next
  150.     
  151.     'Calculate shipping - you might want to pull this out into a function if your shipping 
  152.     ' calculations are more complicated then ours.  ;)
  153.     If sTotal <> 0 Then
  154.         sShipping = 7.5
  155.     Else
  156.         sShipping = 0
  157.     End If
  158.     sTotal = sTotal + sShipping
  159.     %>
  160.     <TR><TD COLSPAN=4 ALIGN="Right"><B>S+H:</B></TD><TD ALIGN="Right">$<%= FormatNumber(sShipping,2) %></TD></TR>
  161.     <TR><TD COLSPAN=4 ALIGN="Right"><B>Total:</B></TD><TD ALIGN="Right">$<%= FormatNumber(sTotal,2) %></TD></TR>
  162.     </TABLE>
  163.     <%
  164. End Sub
  165.  
  166. ' We implemented this this way so if you attach it to a database you'd only need one call per item
  167. Function GetItemParameters(iItemID)
  168. Dim aParameters ' Will contain 3 string values : image path, description, price
  169.                 ' However we need to keep price so it can be converted to a
  170.                 ' single for computation hence no currency symbol.  This array
  171.                 ' can also be expanded to contain any other information about the
  172.                 ' product that you might want to pull from the DB.
  173.     Select Case iItemID
  174.         Case 1
  175.             aParameters = Array("./images/shop_shirt.gif", "ASP 101 T-Shirt", "15.00")
  176.         Case 2
  177.             aParameters = Array("./images/shop_kite.gif", "ASP 101 Kite", "17.50")
  178.         Case 3
  179.             aParameters = Array("./images/shop_watch.gif", "ASP 101 Watch", "35.00")
  180.         Case 4 ' Not in use because we couldn't draw a pen in a few seconds!
  181.             aParameters = Array("./images/shop_pen.gif", "ASP 101 Pen", "5.00")
  182.     End Select
  183. ' Return array containing product info.
  184. GetItemParameters = aParameters
  185. End Function
  186. %>
  187.  
  188.  
  189.  
  190.  
  191. <% ' ***** Begin the infamous runtime script *****
  192. ' Declare our Vars
  193. Dim dictCart ' as dictionary
  194. Dim sAction ' as string
  195. Dim iItemID ' as integer
  196. Dim iItemCount ' as integer
  197.  
  198. ' Get a reference to the cart if it exists otherwise create it
  199. If IsObject(Session("cart")) Then
  200.     Set dictCart = Session("cart")
  201. Else
  202.     ' We use a dictionary so we can name our keys to correspond to our
  203.     ' item numbers and then use their value to hold the quantity.  An
  204.     ' array would also work, but would be a little more complex and
  205.     ' probably not as easy for readers to follow.
  206.     Set dictCart = Server.CreateObject("Scripting.Dictionary")
  207. End If
  208.  
  209. ' Get all the parameters passed to the script
  210. sAction = CStr(Request.QueryString("action"))
  211. iItemID = CInt(Request.QueryString("item"))
  212. iItemCount = CInt(Request.QueryString("count"))
  213. %>
  214. <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>
  215. <TR><TD>
  216. <%
  217. ' Select action based on user input
  218. Select Case sAction
  219.     Case "add"
  220.         AddItemToCart iItemID, iItemCount
  221.         ShowItemsInCart
  222.         %>
  223.         </TD></TR>
  224.         <TR><TD ALIGN="right">
  225.         <A HREF="./shopping.asp?action="><IMG SRC="./images/shop_look.gif" BORDER=0 WIDTH=46 HEIGHT=46 ALT="Continue Looking"></A>
  226.         <A HREF="./shopping.asp?action=checkout"><IMG SRC="./images/shop_checkout.gif" BORDER=0 WIDTH=46 HEIGHT=46 ALT="Checkout"></A><BR>
  227.         <%
  228.     Case "del"
  229.         RemoveItemFromCart iItemID, iItemCount
  230.         ShowItemsInCart
  231.         %>
  232.         </TD></TR>
  233.         <TR><TD ALIGN="right">
  234.         <A HREF="./shopping.asp?action="><IMG SRC="./images/shop_look.gif" BORDER=0 WIDTH=46 HEIGHT=46 ALT="Continue Looking"></A>
  235.         <A HREF="./shopping.asp?action=checkout"><IMG SRC="./images/shop_checkout.gif" BORDER=0 WIDTH=46 HEIGHT=46 ALT="Checkout"></A><BR>
  236.         <%
  237.     Case "viewcart"
  238.         ShowItemsInCart
  239.         %>
  240.         </TD></TR>
  241.         <TR><TD ALIGN="right">
  242.         <A HREF="./shopping.asp?action="><IMG SRC="./images/shop_look.gif" BORDER=0 WIDTH=46 HEIGHT=46 ALT="Continue Looking"></A>
  243.         <A HREF="./shopping.asp?action=checkout"><IMG SRC="./images/shop_checkout.gif" BORDER=0 WIDTH=46 HEIGHT=46 ALT="Checkout"></A><BR>
  244.         <%
  245.     Case "checkout"
  246.         PlaceOrder
  247.         %>
  248.         </TD></TR>
  249.         <TR><TD ALIGN="left">
  250.         <BR>
  251.         <H2>Thank you for your order!</H2>
  252.         If this had been an actual shopping cart, this is where you would have had them enter
  253.         their personal information and payment method and then taken care of any backend
  254.         processing before finalizing their order.  However as this is a shopping cart sample
  255.         and this is where security becomes a problem, we'll leave it for a future sample.
  256.         <%
  257.     Case Else ' Shop
  258.         ShowFullCatalog
  259.         %>
  260.         </TD></TR>
  261.         <TR><TD ALIGN="right">
  262.         <A HREF="./shopping.asp?action=viewcart"><IMG SRC="./images/shop_cart.gif" BORDER=0 WIDTH=46 HEIGHT=46 ALT="View Cart Contents"></A>
  263.         <%
  264. End Select
  265.  
  266. ' Return cart to Session for storage
  267. Set Session("cart") = dictCart
  268. %>
  269. </TD></TR>
  270. </TABLE>
  271.