CreateCart設計:
請看程式碼:
Class CartKit
REM 開始類CartKit的定義
Sub CreateCart()
IF IsArray(Session("Cart")) = false THEN
Dim mudCart(19,1)
Session("Cart") = mudCart
END IF
End Sub
其中:Session("Cart")儲存的就是推車的內容。如果推車不存在,我們就定義一個二維數組mudCart來表達推車,並將它儲存到session("Cart")中。
CheckCart設計:
本函數判斷Cart是否已經建立。比較簡單。
Function CheckCart()
IF IsArray(Session("Cart")) THEN
CheckCart=True
ELSE
CheckCart=False
END IF
End Function
CheckItem設計:
請看代碼:
Function CheckItem(ID)
IF CheckCart=True THEN
varMudCart = Session("Cart")
FOR i = LBound(varMudCart)TO UBound(varMudCart)
IF varMudCart(i,0) = ID THEN
CheckItem=TRUE
Exit Function
ELSEIF varMudCart(i,0) <> ID THEN
CheckItem=FALSE
END IF
NEXT
END IF
End Function
首先判斷推車是否存在。然後將商品的編號ID和購物推車中的商品編號逐一比較。如果有相等的,返回真。否則為假。
AddItem(ID,Qty)設計:
請參考本文的附件CartKit.asp,這裡介紹的函數我們把它們封裝到一個名為CartKit的class中。下面的程式碼片段首先建立了一個CartKit對象,然後檢查推車是否已經存在。如果不存在,就建立推車並新增商品;否則,檢查商品編號是否已經存在於Cart中。如果是,就更新數量;否則,新增商品。
Function AddItem(ID, Qty)
Set CartObj = New CartKit
varCartStatus = CartObj.CheckCart
IF varCartStatus=false THEN
CartObj.CreateCart
mudCart=Session("Cart")
mudCart(0,0)=ID
mudCart(0,1)=Qty
Session("Cart")=mudCart
Exit Function
ELSEIF varCartStatus=true THEN
IF CartObj.CheckItem(ID) = true THEN
CartObj.UpdateItem ID,Qty
ELSEIF cartObj.CheckItem(ID) = false THEN
mudCart = Session("Cart")
FOR i = LBound(mudCart) TO UBound(mudCart)
IF mudCart(i,0) = "" THEN
mudCart(i,0) = ID
mudCart(i,1) = Qty
Session("Cart") = mudCart
Exit Function
END IF
NEXT
END IF
END IF
End Function
UpdateItem設計:
Function UpdateItem(ID, Qty)
mudCart = Session("Cart")
FOR i = LBound(mudCart) TO UBound(mudCart)
IF mudCart(i,0) = ID THEN
mudCart(i,1) = Qty
Session("cart")=mudCart
Exit Function
END IF
NEXT
End Function
ViewItem設計:
Function ViewItem()
mudCart=Session("Cart")
IF isArray(mudCart) THEN
%>
Item Name
Item Quantity
<%
Response.Write "Item ID: " & mudCart(i,0) & "<--"
Response.Write "
"
Response.Write "Item Qty: " & mudCart(i,1) & "<--"
Response.Write "
"
END IF
NEXT
ELSEIF isArray(mudCart) = False THEN
Response.Write "No item in your cart!"
Response.Write "
"
Response.Write "Y don’t you do shop!"
END IF
End Function
RemoveItem(ID)設計:
Function RemoveItem(ID)
mudCart=Session("Cart")
’Find the Postion / Index of given ID.
For i = LBound(mudCart) TO Ubound(mudCart)
IF mudCart(i,0) = ID THEN
Pos=i
Exit For
END IF
Next
’Delete Process...
FOR i = Pos TO UBound(mudCart) - 1
IF Not mudCart(i,0) = "" THEN
mudCart(i,0) = mudCart(i+1,0)
END IF
Next
End Function
End Class REM 結束類CartKit的定義
CartKit.asp
'------建立一輛購物車-----
Class CartKit
Sub CreateCart()
If IsArray(Session("Cart")) = false Then
Dim mudCart(19,1)
Session("Cart") = mudCart
End If
End Sub
Dim myArray(3,1)
ReDim Preserve myArray(3,2)
Dim myArrayID(1), myArrayQty(1)
Function AddItem(ID, Qty)
Set CartObj = New CartKit
varCartStatus = CartObj.CheckCart
If varCartStatus=false Then
CartObj.CreateCart
mudCart=Session("Cart")
mudCart(0,0)=ID
mudCart(0,1)=Qty
Session("Cart")=mudCart
Exit Function
ElseIf varCartStatus=true Then
If CartObj.CheckItem(ID) = true Then
CartObj.UpdateItem ID,Qty
Elseif cartObj.CheckItem(ID) = false Then
mudCart = Session("Cart")
For i = LBound(mudCart) TO UBound(mudCart)
If mudCart(i,0) = "" Then
mudCart(i,0) = ID
mudCart(i,1) = Qty
Session("Cart") = mudCart
Exit Function
End If
Next
End If
End If
End Function
Function RemoveItem(ID)
mudCart=Session("Cart")
For i = LBound(mudCart) TO Ubound(mduCart)
If myArray(i,0) = ID Then
Pos=i
Exit For
End If
Next
For i = Pos TO UBound(myArray) - 1
If Not myArray(i, 0) = "" Then
myArray(i,0) = myArray(i+1,0)
myArray(i,1) = myArray(i+1,1)
End If
Next
End Function
Role of Proxy.asp
varAction=CStr(Request.QueryString("ac"))
Set mudCartObj = New CartKit
varID = CStr(Request.Form("ID"))
varQty = Request.QueryString("qty")
If varAction="by" Then
mudCartObj.AddItem varID, varQty
ElseIf varAction="rm" Then
mudCartObj.RemoveItem varID
ElseIf varAction="up" Then
mudCartObj.UpdateItem varID, varQty
ElseIf varAction="ra" Then
mudCartObj.RemoveAll
ElseIf varAction="co" Then
MudcartObj.CheckOut
ElseIf varAction="cs" Then
MudcartObj.ContinueShopping
End If
Function CartStatus()
varTotalItems = 0
Set CartObj = New CartKit
If CartObj.CheckCart = true Then
MudCart = Session("Cart")
VarTotalItem = 0
For i = Lbound(mudCart) TO Ubound(mudCart)
If mudCart(i,0) <> "" Then
varTotalItem = varTotalItem + Cint(mudCart(i,1))
End If
Next
ElseIf CartObj.CheckCart= False Then
varTotalItem="0"
End If
CartStatus=varTotalItem End Function
Set CartObj = Nothing
End Function
<%="You have " & Response.Write CartObj.CartStatus & _
" items(s) in your cart"%>