| (ASP)Code giỏ hàng |
|
|
|
|
Bài viết này được đóng góp từ bạn Mạnh Tuấn. Đây là code giỏ hàng (ASP), tuy chưa hoàn thiện nhưng có thể sẽ là tài liệu cho các bạn mới tiếp xúc với website TMDT. Vua code duoc cai gio hang suong qua share cho moi nguoi xem va cung gop vai cho nha Dau tien la include cua shoppingcar : de shoppingcart su dung ma thuc hien cac cau lenh File: include-shoppingcart: <!--#include file="connections/config.asp"--> <% openconn Public Sub themsp(pid) strList=Session("ProductList") countList=Session("ProductCount") if (trim (pid)<>"") then if (instr(strList,pid)=0) then'neu khong co ma san pham trong list thi them strList=strList+pid+"," Session("ProductList")=strList CountList=CountList+"1"+"," Session("ProductCount")=CountList else 'co san pham do trong gio hang roi arrProducts=split(Session("ProductList"),",") arrCounts=split(Session("ProductCount"),",") For i = 0 to uBound(arrProducts) if (inStr(arrProducts(i),pid))>0 and flag=false then 'lam sao de refresh k bi tang arrCounts(i)=CStr(Cint(arrCounts(i))+1) flag=true exit for end if Next session("ProductList")=join(arrProducts,",") session("ProductCount")=join(arrCounts,",") end if end if end sub public Sub xoasp(pid) strList=Session("ProductList") countList=Session("ProductCount") if trim(pid)<>"" then if inStr(strList,pid)=0 then exit sub else arrProducts=split(session("ProductList"),",") arrCounts=split(session("ProductCount"),",") For i=0 To uBound(arrProducts) If inStr(arrProducts(i),pid)>0 Then arrCounts(i)="0" arrProducts(i)="0" Exit For End If Next session("ProductList")=join(arrProducts,",") session("ProductCount")=join(arrCounts,",") 'Loai bo cac product co so luong 0 session("ProductList")=Replace(session("ProductList"), "0,", "") session("ProductCount")=Replace(session("ProductCount"), "0,", "") end if end if end Sub Public Sub xoatatca session("ProductList")="" session("ProductCount")="" end Sub Sub UpdateQuantity(pid,Qty) arrProducts=split(session("ProductList"),",") arrCounts=split(session("ProductCount"),",") For j = 0 to UBound(arrProducts) if inStr(arrProducts(j),pid)>0 then arrCounts(j)=trim(Cstr(Qty)) exit for end if Next session("ProductList")=join(arrProducts,",") session("ProductCount")=join( arrCounts,",") End Sub closeconn %> con day la code cua shoppingcart <!--#include file="include-shoppingcart.asp"--> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <link href="/css-pro1.css" media="all" rel="stylesheet" type="text/css" /> <link href="/Picture 037.png" rel="stylesheet" media="all" /> <style type="text/css" media="all"> #a:hover{ text-decoration:underline} </style> <title>Untitled Document</title> </head> <body> <% openconn id = request.querystring("id") action=request.querystring("action") if action="add" then themsp id end if if action="update" then str_qty=request.form("Qty") Qty_arr=split(str_qty,",") str_id=Session("ProductList") id_arr=split(str_id,",") for i=0 to ubound(Qty_arr) UpdateQuantity id_arr(i),Qty_arr(i) Next strList=session("ProductList") strList=Left(strList,(len(strList)-1)) strListArray=split(strList,",") countList=session("ProductCount") countListArray=split(countList,",") end if if(action="xoasp") then xoasp id end if if(action="xoatatca") then RemoveAll end if %> <form name="form1" method="post" action="shoppingcart.asp?action=update"> <table width="100%" border="1" cellpadding="1" cellspacing="0"> <tr style=" height:50px"><td>GIO HANG</td></tr> </table> <table width="100%" border="1" cellpadding="1" cellspacing="0"> <tr align="center" class="tr2"> <td>STT</td> <td>TEN SAN PHAM</td> <td>DON GIA</td> <td>SO LUONG</td> <td>THANH TIEN (VND) </td> <td> </td> </tr> <% dim rs set rs=server.CreateObject("adodb.recordset") if (session("ProductList")="") then response.write("<tr><td colspan=6>Ban chua dat mua mat hang nao</td></tr></table>")%> <br><a href="/phantrang1.asp">Tiep tuc mua hang</a> <% else strList=session("ProductList") strList=left(strList,len(strList)-1) strListArray=split(strList,",") countList=session("ProductCount") countListArray=split(countList,",") set rs=server.CreateObject("ADODB.RecordSet") totalPrice=0 for i=0 to ubound(strListArray) rs.open "select * from sanpham where Madongho='"&strListArray(i)&"'",conn %> <tr> <td><%=i+1%></td> <td><%=rs("Tendongho")%></td> <td><%=rs("Gia")%></td> <td><input type="text" name="Qty" value="<%=countListArray(i)%>" size="10" /></td> <td><%=(Cint(countListArray(i))*rs("Gia"))%></td> <td id="a"><a href="/shoppingcart.asp?id=<%=strListArray(i)%>&action=xoasp">Xoa</a></td> </tr> <% totalPrice=totalPrice+Cint(countListArray(i))*rs("Gia") rs.close next %> <tr> <td colspan="4">Tong tien (VND): </td> <td><%=totalPrice%></td> <td> </td> </tr> <tr> <td colspan="6">Co <strong><%=ubound(strListArray)+1%></strong> san pham trong gio hang cua ban!</td> </tr> <tr> <td colspan="7" align="center"> <input type="submit" name="submit" value="Cap nhat gio hang"/> <input type="button" value="Xoa gio hang" onclick='window.location="shoppingcart.asp?action=removeAll"'/> <input type="button" value="Dat hang" onclick='window.location="Dathang.asp"'/> <input type="button" value="Tiep tuc mua hang" onclick='window.location="phantrang_product.asp"'/> </td> </tr> </table> </form> <% end if %> </div> </div> </div> </body> </html> Cảm ơn bạn Mạnh Tuấn
http://hotrolaptrinh.com
3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved." |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||





