Nói vá» giá» hà ng mình support luôn ?? Äây là hà m mình sưu tầm ngà y trước, rất dá»… hiểu ? Bạn xem qua nhé.
Tức là mình lưu giá» hà ng và o 1 biến Session thuá»™c kiểu mảng 2 chiá»u...mảng 1 sẽ lưu vị trà cá»§a sản phẩm trong giá» hà ng, mảng 2 sẽ lưu từng cái thuá»™c tÃnh và dụ như tên sản phẩm, giá, số lượng ....
+ Thêm giá» hà ng : khi ngưá»i dùng kÃch và o thêm giá» hà ng, bạn sẽ gá»i hà m
add_cart() vá»›i đối số truyá»n và o là cái id cá»§a sản phẩm đó
sau đó bạn
select * from product where id = id truyá»n và o để lấy ra tên sản phẩm, giá sản phẩm đó.
Tiếp theo dùng 1 vòng lặp duyệt qua mảng, kiểm tra xem sản phẩm đó có trong giỠchưa ? Nếu chưa có thì cấp 1 cái mới, nếu có rồi thì tăng số lượng lên 1 đơn vị.
+ Xoá giá» hà ng : đơn giản như bạn là m (dùng cái thuáºt toán hoán vị chá»— hay dùng ở C đó)
+ Cáºp nháºt số lượng: đối số lại là cái id sản phẩm, và số lượng request từ form thì duyệt qua mảng tìm đến từng cái id cần cáºp nháºt rồi cá»™ng cái số lượng hiện tại vá»›i số lượng request được... Nên kiểm tra trước bắt buá»™c giá trị request Ä‘c phải là số > 0
///=============Chung chung là như thế còn đây là code //=====================//
Xem dễ hiểu lắm
|
|
|
//================================================== ============//
<%
openketnoi();
var productId=String(Request.QueryString("productId")) ;
var prPrice;
var prName;
function addCart(ProductId){
//Select bang Products
var rst=Server.CreateObject("ADODB.Recordset");
var strSql="select * from Products where ProductID='" + productId + "'";
rst.Open(strSql,ketnoi,3,3);
//select bang khuyen mai
var rst_km=Server.CreateObject("ADODB.Recordset");
var strSql_km="select * from Khuyenmai where KhDelete=1 and KhuyenmaiID=" + parseInt(rst.Fields("KhuyenmaiID"));
rst_km.Open(strSql_km,ketnoi,3,3);
if((!rst_km.EOF) && (rst.Fields("KhuyenmaiID")!=1)){
prPrice=eval(((rst.Fields("PrPrice")) * (100-rst_km.Fields("KhDiscount")))/100);
}else{
prPrice=rst.Fields("PrPrice");
}
prName=rst.Fields("PrName");
//===========Xu ly cai gio hang`
var productItem =Session("productItem"); //lay ra so cac san pham trong gio
//---Neu chua co cai gio hang` nao`, cap cho 1 cai
if (productItem==null){
productItem=new Array();
}
//----Da chac chan co 1 cai gio hang` (co the rong hoac khong, ko quan trong)
//--- Kiem tra xem SP can` them co trong gio chua
var check=false;
for(i=0;i<productItem.length;i++){
//----Neu co SP roi, cong 1 vao so luong
if('"' + new String(productItem[i][0]) + '"' == '"' + new String(productId) + '"'){
productItem[i][3]=productItem[i][3] + 1;
check=true;
break;
}
}
//---Neu van chua co trong gio, phai them moi
if(!check){
caItem=new Array(5); //mang caItem luu thuoc tinh cua tung phan tu do
caItem[0]=new String(productId);
caItem[1]=new String(prName);
caItem[2]=new String(prPrice);
caItem[3]=1;
productItem[productItem.length]=caItem;
}
//----Cap nhat lai gio hang (chi can lam thao tac nay 1 lan sau khi xu ly xong xuoi cai mang productItem)
Session("productItem")=productItem;
}
function deleteAll()
{
Session("productItem") = null;
Response.Redirect("shoppingcart.asp");
}
//=====Xoa gio hang===\\
function delCart(deleteId){
var deleteId=parseInt(Request.QueryString("deleteId")) ; //Goi ra ID can xoa
var deleteItem = new Array(); //khai bao deleteItem la 1 kieu mang
deleteItem=Session("productItem"); //luu mang la ProductItem
//Response.Write(deleteItem);
//Response.Flush();
if(deleteItem.length==1){
deleteAll();
}
if(deleteId!=parseInt(deleteItem.length)){
for(var i=deleteId;i<deleteItem.length;i++){
deleteItem[i]=deleteItem[i+1];
}
}
deleteItem.length=deleteItem.length-1; //giam do dai xuong 1 don vi
Session("productItem")=deleteItem;
}
//====== Cap nhat gio hang=========\\
function upCart(){
var updateItem=new Array();
updateItem=Session("productItem");
//Response.Write (typeof(updateItem));
//Response.Flush();
for(var j=0;j<updateItem.length;j++){
var sl_new=parseInt(Request.Form("soluong_" + j));
//Response.Write(sl_new);
//Response.Flush();
updateItem[j][3]=sl_new;
}
Session("productItem")=updateItem;
}
%>
|