类来自网络,但有几个缺点,不断修改中
shop_class_inc.asp
————————————————————————————————————
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
'==========================
'购物车类 类名:UserCart
'作者:gameing
'基本原理:此类用服务器Session变量保存商品数据,商品用一个数组表示.此类里面保存了三中商品价格.
'1、2008-11-25修改Function AddItem() by tommy 商品增加在最后
'2、2008-11-25修改by tommy 增加删除商品过程 Sub DelItem()
'3、2008-11-25转换一些数据类型才能运算
'==========================
Class UserCart
'建立购物车
Sub CreateCart()
Dim CFlag
dim mCart(6,0)
CFlag=CheckCart()
if CFlag=false then
For iCount =LBound(mCart,2) to UBound(mCart,2)
mCart(0,iCount)=""
next
Session("UserCart")=mCart
end if
End Sub
'检查购物车
Public Function CheckCart()
IF IsArray(Session("UserCart")) THEN
CheckCart = true
ELSE
CheckCart = false
END IF
End Function
'检查购物车是否为空
Function CheckEmtpy()
dim mCart
mCart=Session("UserCart")
For i =LBound(mCart,2) to UBound(mCart,2)
if mCart(0,i)<>"" then
CheckEmtpy=false
Exit Function
end if
next
CheckEmtpy=True
End Function
'添加商品
Function AddItem(aID,aName,aPrice1,aPrice2,aPrice3,aCount,aImage) '2008-12-25改进 by tommy 商品增加在最后
dim mCart
AddItem=false
if CheckItem(aID)=false then
'无此商品
mCart=Session("UserCart")
i =UBound(mCart,2)
i=i+1
Redim preserve mCart(6,i)
mCart(0,i)=aID
mCart(1,i)=aName
mCart(2,i)=aPrice1
mCart(3,i)=aPrice2
mCart(4,i)=aPrice3
mCart(5,i)=aCount
mCart(6,i)=aImage
session("UserCart")=mCart
AddItem=True
Else
'有此商品,则商品数量+1
ModifItem aID,aCount,0
AddItem=True
End if
End Function
'删除商品数量
Sub DelItem(mID) '改进2008-12-25by tommy 增加删除商品过程
dim mCart
response.Write(mID)
mCart = Session("UserCart")
mount=UBound(mCart,2)
If mID=mount Then
mount=mount-1
mCart(0,mount+1)=""
ReDim Preserve mCart(6,mount)
Else
mount=mount-1
For i=mID To mount
For j=0 To 6
mCart(j,i)=mCart(j,i+1)
Next
Next
mCart(0,mount+1)=""
ReDim Preserve mCart(6,mount)
End If
session("UserCart")=mCart
End Sub
'修改商品数量
Sub ModifItem(mID,mCount,mFlag)
'mFlag-标志 0-添加 1-删除,2-修改 3-清空
'mID-商品ID
'mCount-修改商品的数量
dim mCart
mCart = Session("UserCart")
For i =LBound(mCart,2) to UBound(mCart,2)
if mCart(0,i)=mID then
select case mFlag
case 1
mCart(0,i)=Int(mCart(5,i))-Int(mCount)
case 0
mCart(5,i)= Int(mCart(5,i))+Int(mCount)
case 2
if mCount=0 then
mCart(5,i)=0
mCart(0,i)=""
else
mCart(5,i)=mCount
end if
case 3
mCart(5,i)=0
mCart(0,i)=""
end select
session("UserCart")=mCart
exit Sub
end if
next
End Sub
'查看推车
Function ViewCart()
dim mCart
mCart=session("UserCart")
ViewCart=mCart
End Function
'检查商品
Function CheckItem(cID)
dim mCart
mCart =Session("UserCart")
For i =LBound(mCart,2) to UBound(mCart,2)
if mCart(0,i)=cID then
CheckItem=True
exit Function
end if
next
CheckItem=false
End Function
'清空购物车
Sub RemoveAll()
dim mCart
mCart = Session("UserCart")
For i =LBound(mCart,2) to UBound(mCart,2)
mCart(0,i)=""
next
Session("UserCart")=mCart
End Sub
'商品总价值
Function TPrice()
dim mCart,i
dim OutPrice(3)
mCart=session("UserCart")
for i=LBound(mCart,2) to UBound(mCart,2)
if mCart(0,i)<>"" then
Outprice(0) = Int(Outprice(0)) + Int(mCart(2,i))*Int(mCart(5,i))
Outprice(1) = Int(Outprice(1)) + Int(mCart(3,i))*Int(mCart(5,i))
Outprice(2) = Int(Outprice(2)) + Int(mCart(4,i))*Int(mCart(5,i))
Outprice(3) = Int(OutPrice(3)) + Int(mCart(5,i))
end if
next
TPrice=OutPrice
End Function
End Class
%>
————————————————————————————————————————————
shop.asp
--
<!--#include file="shop_class_inc.asp"-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
</script>
</head>
<body>
<form name="form1" action="shop.asp" method="post">
产品Id:<input type="text" name="produceId" />
产品名称:<input type="text" name="produceName" />
产品单价:<input type="text" name="producePrice" />
产品数量:<input type="text" name="buynum" />
<input type="submit" />
</form>
<br>
<%
dim uCart
If isEmpty(uCart) Then
set uCart= new UserCart
uCart.CreateCart
End If
action=request("action")
If action="" Then
Dim aId,aName,aPrice,aCount
aId=request.Form("produceId")
aName=request.Form("produceName")
aPrice=request.Form("producePrice")
aCount=request.Form("buynum")
aa=uCart.AddItem(aId,aName,aPrice,0,0,aCount,0)
ElseIf action="del" Then
aId=request("produceId")
aa=uCart.DelItem(aId)
response.Redirect("shop.asp")
End If
myCart=uCart.ViewCart
%>
<table border="1">
<tr>
<th>产品编号</th>
<th>产品名称</th>
<th>产品价格</th>
<th>产品数量</th>
<th>操作</th>
</tr>
<%
For i=LBound(myCart,2) To UBound(myCart,2)
If myCart(0,i)<>"" Then '以前的判断语句
%>
<tr>
<td><%=MyCart(0,i)%></td>
<td><%=MyCart(1,i)%></td>
<td><%=MyCart(2,i)%></td>
<td><%=MyCart(5,i)%></td>
<td><a href="shop.asp?action=del&produceId=<%=MyCart(0,i)%>" οnclick="return confirm('你确定要删除吗?');">删除</a></td>
</tr>
<%
End If
Next
myprice=uCart.TPrice()
%>
<tr>
<td>总价格:<%=myprice(0)%></td>
<td colspan="3">产品总数量:<%=myprice(3)%></td>
</tr>
</table>
<br>
<br>
</body>
</html>