 |
|
มันเป็นส่วนในการลบรายการสินค้าที่เราไม่ต้องการ
ออกจากตระกร้าน่ะครับ
อยากให้ช่วยอธิบายการทำงานของ loop นี้
เช่น พอมีการส่งค่ามาจากอุปกรณ์ checkbox
รอบแรก i มีค่าเป็น ....
k = ...
j = ...
ที่สงสัยมากคือ array มิติแรก ที่กำหนดให้เป็น 0 - 3 คือ id , name, price ,qty
พอจบ loop for หลัง next ตัวแรก ทำไมสามารถกำหนดให้เป็น 0 ทั้ง 4 ตำแหน่ง
ได้ด้วยหรือครับ เพราะตามความเข้าใจตำแหน่งของอาเรย์ 2 มิติ น่าจะเป็น
00 01 02 03 ...
10 11 12 13...
20 21 22 23...
30 31 32 33...
ขอบคุณครับ
p = 0
for each i in request.Form("chk_remove")
k = i - p
for j = (k+1) to session("item")
cart(0 , j-1) = cart(0 , j)
cart(1 , j-1) = cart(1 , j)
cart(2, j-1) = cart(2 , j)
cart(3 , j-1) = cart(3 , j)
next
cart(0 , session("item")) = Null
cart(0 , session("item")) = Null
cart(0 , session("item")) = Null
cart(0 , session("item")) = Null
session("item") = session("item") - 1
p = p + 1
next
-----------------------------------------Source Code--------------------------------------------
TEST.ASP
===========================================
<FORM METHOD=POST ACTION="cart.asp">
<INPUT TYPE="checkbox" NAME="product" value="1,name 1,100,1">Product 1<HR>
<INPUT TYPE="checkbox" NAME="product" value="2,name 2,200,1">Product 2<HR>
<INPUT TYPE="checkbox" NAME="product" value="3,name 3,300,1">Product 3<HR>
<INPUT TYPE="checkbox" NAME="product" value="4,name 4,400,1">Product 4<HR>
<INPUT TYPE="submit">
</FORM>
===========================================
CART.ASP
===========================================
<%
if isempty(session("cart")) then
dim cart()
session("item") = 0 'กำหนดค่าเริ่มต้น
else
cart = session("cart")
end if
'เคยช๊อปมาเเล้วยัง
function add(id,name,price,qty)
'เพิ่ม
'''''''''''''''''''''''''''''''''''
bool_add = true
for i = 1 to session("item")
if id = cart(0,i) then 'check product
bool_add = false
cart(3,i) = cart(3,i) + int(qty)
exit for
end if
next
'----------------------------
if bool_add then
session("item") = session("item") +1
redim preserve cart(3,session("item"))
cart(0,session("item")) =id
cart(1,session("item")) =name
cart(2,session("item")) =price
cart(3,session("item")) =qty
end if
add = cart
'----------------------------
end function
if not isempty(request.Form("product")) then
for each product in request.Form("product")
arr = split( product , "," )
session("cart")=add( arr(0),arr(1),arr(2),arr(3) )
next
response.Redirect("cart.asp")
end if
if not isempty(request.Form("recal")) then
'----------------------------------------
for i=1to session("item")
if not isNumeric (request.Form("txt_qty")(i) ) then
response.Write("<script>alert('ตัวเลข');window.location='cart.asp'</script>")
response.End()
elseif request.Form("txt_qty")(i) < "1" then
response.Write("<script>alert('<<<<<< 1');window.location='cart.asp'</script>")
response.End()
else
cart(3,i) = request.Form("txt_qty")(i)'ใช้ระบุตำเเหน่ง .ในกรณีที่ ชื่อเหมือนกาน
end if
next
'update--------------------
p = 0
for each i in request.Form("chk_remove")
k = i - p
for j = (k+1) to session("item")
cart(0 , j-1) = cart(0 , j)
cart(1 , j-1) = cart(1 , j)
cart(2, j-1) = cart(2 , j)
cart(3 , j-1) = cart(3 , j)
next
cart(0 , session("item")) = Null
cart(0 , session("item")) = Null
cart(0 , session("item")) = Null
cart(0 , session("item")) = Null
session("item") = session("item") - 1
p = p + 1
next
session("cart") = cart
response.Redirect("cart.asp")
end if
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874">
</head>
<body><form action="cart.asp" method="post">
<table width="400" border="1" align="center" cellpadding="5" cellspacing="0">
<tr>
<td colspan="5"><div align="center">shopping cart </div></td>
</tr>
<tr>
<td width="44"><div align="center">remove</div></td>
<td width="32"><div align="center">name</div></td>
<td width="80"><div align="center">price</div></td>
<td width="67"><div align="center">Qty</div></td>
<td width="115"><div align="center">Sub Total </div></td>
</tr>
<%
Q = 0
S = 0
for i = 1 to session("item")
Q =Q + int(cart(3,i))
S =S + (cart(3,i)*cart(2,i))
%>
<tr>
<td><div align="center">
<input type="checkbox" name="chk_remove" value="<%=i%>">
</div></td>
<td><img src="image/<%=cart(0,i)%>.jpg"><br><%=cart(1,i)%></td>
<td><div align="center"><%=cart(2,i)%></div></td>
<td>
<div align="right">
<input name="txt_qty" value="<%=cart(3,i)%>" type="text" size="5" maxlength="5">
</div></td><td><div align="right"><%=cart(3,i)*cart(2,i)%></div></td>
</tr>
<%next
if S >=50000 then
session("shipping") = 0
elseif S >= 10000 and S <= 49999 then
session("shipping") = S*0.01 'เปง %
elseif S >= 1000 and S <= 4999 then
session("shipping") = S*0.5
else
session("shipping") = S*0.10
end if
%>
<tr>
<td colspan="3">subtotal</td>
<td><div align="right"><%=Q%></div></td>
<td><div align="right"><%=S%></div></td>
</tr>
<tr>
<td colspan="3">shopping cart </td>
<td> </td>
<td><div align="right"><%=session("shipping")%></div></td>
</tr>
<tr>
<td colspan="3">total</td>
<td> </td>
<td><div align="right"><%=session("shipping")+S%></div></td>
</tr>
<tr>
<td colspan="5"><div align="center">
<input type="button" onClick="window.location='test.asp'" value="<<continue shop">
<input type="submit" name="recal" value="recalculale">
<input type="button" onClick="window.location='checkoutform.asp'" value="check out>>">
</div>
</td>
</tr>
</table>
</form>
</body>
</html>
===================================================== 00
Tag : - - - -
|
|
 |
 |
 |
 |
Date :
13 ก.ย. 2548 10:16:13 |
By :
คอยอ่าน |
View :
2714 |
Reply :
0 |
|
 |
 |
 |
 |
|
|
|
 |