我得到了一个购物车代码,它使用二维数组在会话中存储用户订单!
我们这里有一些常量:
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!
输出是这样的:
产品 ID = 1
产品名称 = 奶酪
产品价格 = 1 !!!!!!!!!
产品数量 = 1
请指导我!:(