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を見せてください!!
出力は次のようになります。
商品 ID = 1
商品名 = チーズ
商品価格 = 1 !!!!!!!
商品の数量 = 1
ガイドしてください! :(