<% ' SHOPPING CART COOKIE LIBRARY ' BRANDON McLAMB ' IDESIGNS (c) 2000 ' Perform the actions if the user requested to add or remove items from the cart 'If Len(Request.QueryString("addtocart"))>0 Then ' If NOT(IsInCart(Request.QueryString("addtocart"))) Then addToCart(Request.QueryString("addtocart")) ' 'ElseIf Len(Request.QueryString("removefromcart"))>0 Then ' If(IsInCart(Request.QueryString("removefromcart"))) Then RemoveFromCart(Request.QueryString("removefromcart")) ' 'End If ' This function returns a comma delimated list of the items in the cart Function getCartList() getCartList = Request.Cookies("CART")("ITEMS") End Function ' This function returns the number of items in the cart Function itemsInCart() ' Declare the variables dim items ' If the cookie exists, then get the number of items in the cart If (Request.Cookies("CART").HasKeys) Then items = Split(Request.Cookies("CART")("ITEMS"),",") itemsInCart=ubound(items)+1 ' If the cookie does not exist, then the number of items in the cart must be zero Else itemsInCart=0 End If End Function ' This function determines if an item is in the cart Function IsInCart(ID) ' Declare the variables dim item ' If the cookie exists, determine if the product is in the cart If (Request.Cookies("CART").HasKeys) Then IsInCart=False For Each item In Split(Request.Cookies("CART")("ITEMS"),",") If cInt(item)=cInt(ID) Then IsInCart=True Exit For End If Next ' Else, if the cookie does not exist, it cannot be in the cart Else IsInCart=False End If End Function ' This function removes an ID from the cart 'Function removeFromCart(ID) ' ' ' Declare the variables ' dim item, newcart ' ' ' Loop through all of the items in the cart ' For Each Item In Split(Request.Cookies("CART")("ITEMS"),",") ' ' ' If the item does not equal the ID to remove, keep it ' if cInt(Item)<>cInt(ID) Then ' newcart = newcart & Item & "," ' End If ' ' Next ' ' If Right(newcart,1)="," Then newcart = Left(newcart,len(newcart)-1) ' Response.Cookies("CART")("ITEMS") = newcart ' 'End Function ' This function adds an ID to the cart 'Function addToCart(ID) ' ' ' If the item is not already in the cart, then add it ' If NOT(IsInCart(ID)) Then ' ' ' If there are more than 1 items in the cart, apend this item ' If (Len(Request.Cookies("CART")("ITEMS"))>0) Then ' Response.Cookies("CART")("ITEMS") = Request.Cookies("CART")("ITEMS") & "," & ID ' ' ' Else, just set the items equal to this item ' Else ' Response.Cookies("CART")("ITEMS") = ID ' ' End If ' ' End If 'End Function ' This function generates a link to add the item to the cart 'Function addLink(ID) ' addLink = Request.ServerVariables("SCRIPT_NAME") & "?" & generateQuery & "addtocart=" & ID 'End Function ' This function generates a link to remove the item from the cart 'Function removeLink(ID) ' removeLink = Request.ServerVariables("SCRIPT_NAME") & "?" & generateQuery & "removefromcart=" & ID 'End Function ' This function removes the addtocart and removefromcart variables from the query string and generates ' a new query string. 'Function generateQuery ' ' ' Declare the variables ' dim item, values, newquery ' ' ' Set the new query string equal to nothing ' newquery = "" ' ' ' Loop through the elements and write only those that aren't removefromcart or addtocart ' For Each Item In Split(Request.QueryString,"&") ' values = Split(Item,"=") ' If NOT(UCASE(values(0))="REMOVEFROMCART") AND NOT(UCASE(values(0))="ADDTOCART") Then ' newquery = newquery & Item & "&" ' End If ' next ' ' ' Set the function equal to the value ' generateQuery = newquery ' 'End Function %>