一个操作购物车的程序(ASP)

<%

'数据格式:商品ID|||商品数量|||其他~~~~~下一个记录

%>
<%
class shoppingCar
  public proid
  public proNum
  public other
  Private shoppingStr
  Private cars()
  Private hand
 
  public function open(str)'读入数据字符串
  shoppingStr=str
  hand=-1
  if shoppingStr="" or isNull(shoppingStr) Then
  else
     tempCar=split(shoppingStr,"~~~~~")
  ReDim cars(ubound(tempCar))
  for i=0 to ubound(tempCar)
     cars(i)=tempCar(i)
  next
  end if
  end function
 
  public function addshopping(shoppingId,shoppingNum,shoppingOther)'添加商品进入购物车
  if shoppingStr="" Then
     shoppingStr=shoppingId & "|||" & shoppingNum & "|||" & shoppingOther
  else
     do while getNext()=1
     add=1
     if proid=shoppingId and shoppingOther=Other Then
       add=0
       changeNum hand,Clng(proNum)+1
     end if
  loop
  hand=-1
  if add=1 Then
       shoppingStr=shoppingStr & "~~~~~" & shoppingId & "|||" & shoppingNum & "|||" & shoppingOther
  end if
  end if
  if shoppingStr="" or isNull(shoppingStr) Then
     else
        tempCar=split(shoppingStr,"~~~~~")
     ReDim cars(ubound(tempCar))
     for i=0 to ubound(tempCar)
       cars(i)=tempCar(i)
     next
       end if
  end function
 
  public function getNext()'进入下一条记录,并返回是否结束
    if shoppingStr="" Then
    getNext=0
 else
    hand=hand+1
    if hand>getCarLen() Then
      getNext=0
    else
      getnext=1
   tempArray=split(cars(hand),"|||")
      proId=tempArray(0)
   proNum=tempArray(1)
   Other=tempArray(2)
    end if
 end if
  end function
 
  public function delShopping(k)'按排序号删除商品
     delShopping=0
  if shoppingStr="" Then exit function
  if k<0 or k>getCarLen Then exit function
  if shoppingStr="" Then
  else
     for i=k to ubound(cars)-1
           cars(i)=cars(i+1)
        next
        redim Preserve cars(ubound(cars)-1)
        shoppingStr=join(cars,"~~~~~")
  delShopping=1
  end if
  end function
 
  public function delShoppingbyId(id)'按商品ID删除商品
     delShoppingbyId=0
  for i=ubound(car) to 0 step -1
     tempid=split(cars(i),"|||")(0)
  if tempid=id Then
      for j=i to ubound(cars)-1
      cars(j)=cars(j+1)
   next
   redim Preserve cars(ubound(cars)-1)
   shoppingStr=join(cars,"~~~~~")
   delShoppingbyId=1
  end if
  next
  end function
 
  public function changeNum(k,num)'按排序位改变商品数量
     changeNum=0
  if k<0 or k>getCarLen() Then exit function
  tempArray=split(cars(k),"|||")
  tempid=tempArray(0)
  tempNum=tempArray(1)
  tempOther=tempArray(2)
  tempNum="" & num
  cars(k)=tempid & "|||" & tempNum & "|||" & tempOther
  shoppingStr=join(cars,"~~~~~")
  changeNum=1
  end function
 
  function changeNumById(id,num)'按商品ID改变商品数量
     changeNumById=0
  if shoppingStr="" Then exit function
  for i=ubound(car) to 0 step -1
     tempArray=split(cars(k),"|||")
     tempid=tempArray(0)
     tempNum=tempArray(1)
     tempOther=tempArray(2)
  if tempid=id Then
      tempNum="" & num
         cars(k)=tempid & "|||" & tempNum & "|||" & tempOther
   shoppingStr=join(cars,"~~~~~")
   delShoppingbyId=1
  end if
  next
  end function
 
  public function getCarLen()'获得购物车长度
    if shoppingStr="" Then
   getCarLen=-1
 else
   getCarLen=ubound(cars)
 end if
  end function
 
  public function getHand()'取得当前句柄的位置
    getHand=hand
  end function
 
  public function save()'保存(读出)当前数据字符串
    save=shoppingStr
  end function
end class
%>

 

自己做的,贴出来希望大家给点意见

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用: 第1 '================================================= '建立购物车对象,该对象用于直接在程序中调用 '================================================= dim uCart set uCart= new UserCart 第二 建立一个购物车 uCart.CreateCart (可以重复建立,因为里面有IsArray判断。所以建议这句在建立购物车对象后必写) 第三 增加购物车里的商品,在客户端点了某产品后,服务器端处理的ASP文件中接受传过来的产品标志,并访问数据库。分别把AddItem(aID产品标 志如ID,aName产品名称,aPrice1产品价格一,如单价,aPrice2产品价格二如会员价,aPrice3产品价格三如金牌会员价,如果没这么多可以置空 或置0,aCount购买数量,一般是一个,多个的话后面可以用修改函数修改,aImage产品图片地址) 使用方法:aa=uCart.AddItem(aID产品标志如ID,aName产品名称,aPrice1产品价格一,如单价,aPrice2产品价格二如会员价,aPrice3产品价格 三如金牌会员价,如果没这么多可以置空或置0,aCount购买数量,一般是一个,多个的话后面可以用修改函数修改,aImage产品图片地址),返回 true表示成功,false表示失败 第四 增加了以后进如显示页面,就要用到查看购物车 mycart=uCart.ViewCart() For i =LBound(myCart,2) to UBound(myCart,2) if myCart(0,i)"" then myCart(0,i) '获取标号 myCart(1,i) '获取单价 。。。以此类推 end if next 第五 查看了,可以修改购物车,如更改数量等,或是删除其中的 call uCart.ModifItem(mID唯一标志号,mCount产品数量,mFlag-标志 0-添加 1-删除,2-修改 3-清空) '先用给后面参数赋值 修改其中的商品 可以用第四个显示,先接受session的值,然后循环修改 或清空购物车 uCart.RemoveAll() 然后结帐,很简单 myprice=uCart.TPrice() 然后myprice(0)是产品单价的总价格,myprice(1)是产品会员价的总价格,myprice(2)是高级会员的总价格,myprice(3)是产品总数量 将商品装入购物车,这时需要用cookie或session来做一个不同页面间传递的全局变量,也就是说关了浏览器(针对session)或清楚了cookie等原因,本次购物车会消失,就象你今天在商场买了一车的东西,最后没结帐,明天肯定没了,又归位了,当然要有特殊需要保存,可以写数据库!所以这里记录的只需要是该商品的相关信息就可以了,这里我们记录他的 物品ID, 物品单价, 物品名称, 物品数量
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值