2012-08-09 54 views
0


我有一个购物车的代码,使用二维数组来存储用户订单在会话中!
我们有一些常量这里:二维数组 - 不更新数量,因为我想

CONST CART_Product_ID = 0 
CONST CART_Product_NAME = 1 
CONST CART_Product_PRICE = 0 
CONST CART_Product_QUANTITY = 3 


我的第一个问题就在这里!
为什么定义这些常量?为什么得到价值0-1-0-3?!

下一步

得到它的变量从一种形式是这样的:

Dim Product_ID, Product_Name, Product_Price, Product_Qty 
Product_ID  = trim(request("pro_id")) 
Product_Name = trim(request("pro_name")) 
Product_Price = trim(request("pro_price")) 
Product_Qty  = trim(request("pro_qty")) 


并在接下来的步骤中,检查出存在会话,如果不创建一个:

If not isArray(Session("Cart")) then 
    dim localCart(4,50) 
ELSE 
    localCart = Session("Cart") 
End If 


在下一步它检查数组添加或更新像这样的项目数量:

Dim FoundIt, i 
if Product_ID <> "" then 
    FoundIt = False 
    For i = 0 to ubound(localCart) 
     If localCart(CART_Product_ID,i) = Product_ID then 
      localCart(CART_Product_QUANTITY,i) = localCart(CART_Product_QUANTITY,i)+1 
      FoundIt = true 
      EXIT For 
     End If 
    NEXT 
    If NOT FoundIt then 
     For i = 0 to ubound(localCart,2) 
      If localCart(CART_Product_ID,i) = "" Then 
       localCart(CART_Product_ID,i) = Product_ID 
       localCart(CART_Product_NAME,i) = Product_Name 
       localCart(CART_Product_PRICE,i) = Product_Price 
       localCart(CART_Product_QUANTITY,i) = Product_Qty 
       EXIT For 
      End If 
     NEXT 
    End If 
End If 

Session("Cart") = localCart 


还有一些问题在这里!
1-当我发布一个相同的产品到这个页面(product_ID = 1),它增加了更多而不是更新数量,为什么?

2时,我想写这样的阵列的内容:

Dim OrderTotal 
OrderTotal = 0 
for i = 0 to ubound(localCart,2) 
    if localCart(CART_Product_ID, i) <> "" then 
     orderTotal = orderTotal + (localCart(CART_Product_PRICE,i)) * localCart(CART_Product_QUANTITY,i) 
     Response.Write("Product ID = "& localCart(CART_Product_ID,i) &"<br/>") 
     Response.Write("Product Name = "& localCart(CART_Product_NAME,i) &"<br/>") 
     Response.Write("Product Price = "& localCart(CART_Product_PRICE,i) &"<br/>") 
     Response.Write("Product Qty= "& localCart(CART_Product_QUANTITY,i) &"<br/>") 
    end if 
next 
Response.Write ("Total = "& formatCurrency(orderTotal) &"") 


它,而不是价格,告诉我产品ID!
oupput是这样的:

产品ID = 1
产品名称=奶酪
产品价格= 1 !!!!!!!
产品数量= 1

请指导我! :(

+0

当你改变CART_Product_PRICE = 2? – stark 2012-08-09 02:18:09

+0

由于斯塔克!它的工作会发生什么,谢谢你,谢谢:) – MAY3AM 2012-08-09 02:46:55

+0

看起来你有一些不好的代码。 – stark 2012-08-09 02:48:10

回答

0

不是一个伟大的设计。这四个常数充当表索引所以他们需要的是四个不同的值。

CONST CART_Product_ID = 0 
CONST CART_Product_NAME = 1 
CONST CART_Product_PRICE = 2 <= this one was wrong 
CONST CART_Product_QUANTITY = 3