footleg

导航

基于cookies的小型购物车程序

基于cookies的小型购物车程序

最近因为公司项目开发,需要一个购物车程序。
虽然网上很多,但个人很久前就想写个。于是马上动手。
代码有点多,感觉方法不怎么好!
随便post上来,高手可略过~!

mycart.asp客户端:
<form method="POST" action="mycart.asp">
<table border="0" width="489" cellspacing="1" id="table1">
 <tr>
  <td bgcolor="#C0C0C0">产品名称</td>
  <td bgcolor="#C0C0C0">数量</td>
  <td bgcolor="#C0C0C0">合价</td>
  <td bgcolor="#C0C0C0">操作</td>
 </tr>
 
 <%
 Nid=split(request.cookies("myCartInfo")("proID"),"|") '商品ID
 Nnum=split(request.cookies("myCartInfo")("num"),"|")  '订购数量
 Nprice=split(request.cookies("myCartInfo")("price"),"|") '单价
 
 for k=1 to ubound(Nid)+1
 %>
 <tr>
  <td>
  <%
  set rs=conn.execute("select productName from product where id="&Nid(k-1)&"")
  response.write rs(0)
  set rs=nothing
  %>
  </td>
  <td><input type="text" name="num" size="4" value="<%=Nnum(k-1)%>"></td>
  <td>&yen;<%=Nprice(k-1)*Nnum(k-1)%></td>
  <td><a href="mycart.asp?action=remove&location=<%=k-1%>">移除</td>
 </tr>
 <%next%>
 <tr>
  <td colspan="4">
  <p align="center">
  <input type="submit" value="清空购物车" name="action">
  <input type="submit" value="继续购物" name="action">
  <input type="submit" value="更新数量" name="action">
  <input type="submit" value="去结帐" name="action">
  </td>
  </tr>
    </table>
</form> 


mycart.asp服务端:

<%
dim action
action=request("action")

select case action

'添加记录
'从产品展示页面“购买”链接过来的
case "add"
productID=HTMLEncode(request.querystring("id"))
set rs=conn.execute("select * from product where id="&productID&"")
if rs.eof then
set rs=nothing
response.write("找不到相应产品!")
response.end
end if

'截取
theid=split(request.cookies("myCartInfo")("proID"),"|")
thenum=split(request.cookies("myCartInfo")("num"),"|")
theprice=split(request.cookies("myCartInfo")("price"),"|")


'清空
response.cookies("myCartInfo")=""


'判断
select case ubound(theid)
 case "-1"
   response.cookies("myCartInfo")("proID")=productID   '产品ID
   response.cookies("myCartInfo")("num")=1   '订购数量
   response.cookies("myCartInfo")("price")=rs("price2")   '单价
   response.cookies("myCartInfo").Expires=Date+7
 case "0"
   if theid(0)=productID then
   response.cookies("myCartInfo")("proID")=theid(0)
   response.cookies("myCartInfo")("num")=thenum(0)+1
   response.cookies("myCartInfo")("price")=theprice(0)
   response.cookies("myCartInfo").Expires=Date+7
   else
   response.cookies("myCartInfo")("proID")=theid(0)&"|"&productID   '产品ID
   response.cookies("myCartInfo")("num")=thenum(0)&"|"&"1"   '订购数量
   response.cookies("myCartInfo")("price")=theprice(0)&"|"&rs("price2")   '单价
   response.cookies("myCartInfo").Expires=Date+7
   end if
 case else
   for mm=1 to ubound(theid)+1
    if theid(mm-1)=productID then '此产品ID己存在
      if request.cookies("myCartInfo")("proID")="" then
        response.cookies("myCartInfo")("proID")=theid(mm-1)
        response.cookies("myCartInfo")("num")=thenum(mm-1)+1
        response.cookies("myCartInfo")("price")=theprice(mm-1)
      else
        response.cookies("myCartInfo")("proID")=request.cookies("myCartInfo")("proID")&"|"&theid(mm-1)
        response.cookies("myCartInfo")("num")=request.cookies("myCartInfo")("num")&"|"&thenum(mm-1)+1
        response.cookies("myCartInfo")("price")=request.cookies("myCartInfo")("price")&"|"&theprice(mm-1)
      end if
      response.cookies("myCartInfo")(""&productID&"hasadd")=1
      response.cookies("myCartInfo").Expires=Date+7
    else
      if request.cookies("myCartInfo")("proID")="" then
        response.cookies("myCartInfo")("proID")=theid(mm-1)
        response.cookies("myCartInfo")("num")=thenum(mm-1)
        response.cookies("myCartInfo")("price")=theprice(mm-1)
        response.cookies("myCartInfo").Expires=Date+7
      else
        response.cookies("myCartInfo")("proID")=request.cookies("myCartInfo")("proID")&"|"&theid(mm-1)   '产品ID
        response.cookies("myCartInfo")("num")=request.cookies("myCartInfo")("num")&"|"&thenum(mm-1)  '订购数量
        response.cookies("myCartInfo")("price")=request.cookies("myCartInfo")("price")&"|"&theprice(mm-1)   '单价
        response.cookies("myCartInfo").Expires=Date+7
      end if
    end if
   next
  
        if request.cookies("myCartInfo")(""&productID&"hasadd")<>"1" then
        response.cookies("myCartInfo")("proID")=request.cookies("myCartInfo")("proID")&"|"&productID   '产品ID
        response.cookies("myCartInfo")("num")=request.cookies("myCartInfo")("num")&"|"&"1"  '订购数量
        response.cookies("myCartInfo")("price")=request.cookies("myCartInfo")("price")&"|"&rs("price2")   '单价
        response.cookies("myCartInfo").Expires=Date+7
        end if
       
end select
set rs=nothing
response.redirect("mycart.asp")


'移除某一商品
case "remove"
theid=split(request.cookies("myCartInfo")("proID"),"|")
thenum=split(request.cookies("myCartInfo")("num"),"|")
theprice=split(request.cookies("myCartInfo")("price"),"|")

response.cookies("myCartInfo")("proID")=""
response.cookies("myCartInfo")("num")=""
response.cookies("myCartInfo")("price")=""

for kk=1 to ubound(theid)+1
if theid(kk-1)=theid(request.querystring("location")) then
else
  if kk=1 then
    response.cookies("myCartInfo")("proID")=theid(kk-1)
    response.cookies("myCartInfo")("num")=thenum(kk-1)
    response.cookies("myCartInfo")("price")=theprice(kk-1)
  else
    if request.cookies("myCartInfo")("proID")="" then
    response.cookies("myCartInfo")("proID")=theid(kk-1)
    response.cookies("myCartInfo")("num")=thenum(kk-1)
    response.cookies("myCartInfo")("price")=theprice(kk-1)
    else
    response.cookies("myCartInfo")("proID")=request.cookies("myCartInfo")("proID")&"|"&theid(kk-1)
    response.cookies("myCartInfo")("num")=request.cookies("myCartInfo")("num")&"|"&thenum(kk-1)
    response.cookies("myCartInfo")("price")=request.cookies("myCartInfo")("price")&"|"&theprice(kk-1)
    end if
  end if
end if
next
response.redirect("mycart.asp")


case "更新数量"
response.cookies("myCartInfo")("num")=""
newNum=HTMLEncode(request.form("num"))

thenum=split(newNum,",")
if ubound(thenum)=0 then
  response.cookies("myCartInfo")("num")=thenum(0)
else
  for p=1 to ubound(thenum)+1
   if p=1 then
   response.cookies("myCartInfo")("num")=thenum(p-1)
   else
   response.cookies("myCartInfo")("num")=request.cookies("myCartInfo")("num")&"|"&trim(thenum(p-1))
   end if
  next
end if
response.redirect("mycart.asp")

case "去结帐"
'转到要求顾客填写收货信息的页面
'在continueBuy.asp页面要调用订购信息的话,可直接向cookies中调用。调用方法与本页方法类似。
response.redirect("continueBuy.asp?action="&server.urlencode("去结帐"))
response.end

case "清空购物车"
response.cookies("myCartInfo")=""

case "继续购物"
response.Redirect("product.asp")

end select
%>

posted on 2007-10-28 17:54  迷途  阅读(1147)  评论(0编辑  收藏  举报