session 购物车类 dictionary

asp函数

2009-05-22 11:24

<!-- #include file="shop.asp" -->
<%
act=request.QueryString("act")
id=request.QueryString("id")
n=request.QueryString("n")


if act="up" then
shop.Edit id,n
end if

if act="del" then
shop.remove(id)
end if

if act="delall" then
shop.removeall()
end if


response.Write("<br>购物清单<br>------------------------------<br>")
for each s in session("shop")
    response.Write("<br>商品id:"&session("shop").item(s).item("id"))
response.Write("<br>商品名称:"&session("shop").item(s).item("title"))
response.Write("<br>数量:<input name='u"&session("shop").item(s).item("id")&"' type='text' size='5' value='"&session("shop").item(s).item("num")&"'/>")
response.Write("<br>价格:"&session("shop").item(s).item("price"))
response.Write("<br><a href='javascript:void(0);' onclick=up('"&session("shop").item(s).item("id")&"')>更新</a>")
response.Write("<br><a href='?act=del&id="&session("shop").item(s).item("id")&"'>删除</a>")
    response.Write("<br>------------------------------<br>")
next


response.Write("共有商品:"&shop.num())
response.Write("<br>")
response.Write("总金额:"&shop.money())


'遍历方法
'for each s in b
'for each ss in b.item(s)
'   response.Write(b.item(s).item(ss))
'next
'    response.Write("<br>")
'next


%><br><a href="?act=delall">清空购物车</a><br><br>
<a href="index.asp">继续购物</a>


<script language="javascript">
function up(id){
n=document.getElementById("u"+id).value;
window.location.href="buy.asp?act=up&id="+id+"&n="+n+"";
window.location.reload();
}
</script>

----------------------------------------------------------------------------------------------------------------

<!-- #include file="shop.asp" -->
<%
id=request.QueryString("id")
t=request.QueryString("t")
m=request.QueryString("m")
if id<>"" then
shop.add id,t,1,m
response.Write("<script>alert('添加商品成功!');location.href='buy.asp';</script>")
response.End()
end if

%><br><br><br>
<table width="200" border="1">
<tr>
    <td><a href="?id=1&T=苹果&m=10">苹果</a></td>
</tr>
<tr>
    <td><a href="?id=2&T=香蕉&m=15">香蕉</a></td>
</tr>
<tr>
    <td><a href="?id=3&T=菠萝&m=3">菠萝</a></td>
</tr>
<tr>
    <td><a href="?id=4&T=大枣&m=1">大枣</a></td>
</tr>
</table>
<a href="buy.asp">购物车</a>

----------------------------------------------------------------------------------------------------------------------

<%
class myShop

public sub Class_Initialize()
    if isempty(session("shop")) then
    Set session("shop") = server.CreateObject("Scripting.Dictionary")
    end if
end sub


'增加
public function Add(id,title,num,price)
    if not session("shop").exists(id) then
        Set car=server.CreateObject("Scripting.Dictionary")
        car.add "id",id
        car.add "title",server.HTMLEncode(title)
        car.add "num",num
        car.add "price",price
        session("shop").add id,car
        set car=Nothing
    end if
end function


'更新
public function Edit(id,num)
    if num > 0 then
      if session("shop").exists(id) then
        session("shop").item(id).item("num")=num
      end if
    else
        remove(id)
    end if
end function

'删除
public function Remove(id)
    if session("shop").exists(id) then session("shop").remove(id)
end function

'清空
public function Removeall()
    session("shop").removeall
end function

'商品数量
public function Num()
    dim sum:sum=0
    for each so in session("shop")
        sum=sum+session("shop").item(so).item("num")
    next
    Num=sum
end function

'商品总价
public function Money()
    dim sum:sum=0
    for each so in session("shop")
        sum=sum+session("shop").item(so).item("price")*session("shop").item(so).item("num")
    next
    Money=sum
end function

'注销
private sub Class_Terminate()

end sub

end class

dim shop
set shop=New myshop

%>