我有一个购物车代码,使用2D数组在会话中存储用户订单!
我们在这里有一些常量:
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 :(得分: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