購物車

創(chuàng)新互聯(lián)建站專注于吉隆網站建設服務及定制,我們擁有豐富的企業(yè)做網站經驗。 熱誠為您提供吉隆營銷型網站建設,吉隆網站制作、吉隆網頁設計、吉隆網站官網定制、微信小程序服務,打造吉隆網絡公司原創(chuàng)品牌,更為您提供吉隆網站排名全網營銷落地服務。
有兩種實現(xiàn)方式,一種是保存在數(shù)據(jù)庫,另外一種是session
保存在數(shù)據(jù)庫的不會以為關閉瀏覽器而消失,session會因為關閉瀏覽器就沒有了。
原理是把每個商品的信息存到一個數(shù)組里面,然后以這個商品的id作為鍵值,然后吧數(shù)組存到session里面就行,
如果是存入數(shù)據(jù)庫的話,就用關聯(lián)數(shù)據(jù)存一下就行的
我也要弄一個這種購物車,
我去寫個,貼出來,【嘿嘿,今天上午新寫的】。
我懶得新建數(shù)據(jù)庫,用的是我的數(shù)據(jù)庫。
你按照我的改一下就能用了
本人水平有限,高手請指正。
你,大,爺?shù)模m然不咋地,保證能用
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
經過調試,
//$my-add_cart(45,3,"茶幾系列");//新增購物
//$my-updata_cart(13,13,8); //更新購物
//$my-del_cart(12,5,'Guest'); //刪除一種購物
//$my-empty_cart('Guest'); //清空購物車
$ok=$my-get_cart('Guest'); //返回購物車
這些都可用
-------------------------------------------------------------------
?php
class Cart
{
public $totalCost=0; //商品總金額
function cart($host,$usr,$pwd,$db)
{
mysql_connect($host,$usr,$pwd) or die(mysql_error);
mysql_select_db($db) or die(mysql_error);
mysql_query("SET Names GBk");
//只要有人訪問,就自動清除一天前所有沒付款的訂單;
$sql="delete FROM shopcart WHERE TO_DAYS( NOW( )) - TO_DAYS( ptime ) =1 and payment=0";
mysql_query($sql);
}
// 彈出提示
function alter($Str,$Url)
{
echo "Script language='JavaScript' alert('".$Str."');/Script";
echo "meta http-equiv=refresh content=0;URL=".$Url."";
}
//增加購物;三個參數(shù):pid:產品ID,ptl:產品數(shù)量,pcid:產品類別
//查詢數(shù)據(jù)庫,是否存在此人在本日內訂過本產品
//如果訂過,那么數(shù)量累加,否則插入一個數(shù)據(jù)庫行
function add_cart($pid,$ptl=1,$pcid)
{
if($ptl=100 || $ptl=0)
{
$this-alter("最多買99件,最少1件","index.php");
die();
}
if(!$_SESSION['usr']) { $usr='Guest';}
else { $usr=$_SESSION['usr'];}
$sql="select * from shopcart where pid='".$pid."' and usr='".$usr."' and pcid='".$pcid."'";
$ex=mysql_query($sql);
$ex1=mysql_fetch_array($ex);
if(!$ex1)
{
$sql="select * from product where ID='".$pid."' and class1='".$pcid."'";
$ok=mysql_query($sql);
$rs=mysql_fetch_array($ok);
if($rs)
{
$totalCost= $rs['Price'] * $ptl;
$sql="insert into shopcart(usr,pid,pname,ptl,price,pcid,psum,payment) Values(";
$sql.="'".$usr."',";
$sql.="'".$rs['ID']."',";
$sql.="'".$rs['Name']."',";
$sql.="'".$ptl."',";
$sql.="'".$rs['Price']."',";
$sql.="'".$rs['Class1']."',";
$sql.="'".$totalCost."',";
$sql.="'0')";
mysql_query($sql) or die(mysql_error());
if($ok) { $this-alter("購物成功","index.php"); }
else { $this-alter("購物失敗","index.php"); }
}
else
{
$this-alter("不存在的商品,或者參數(shù)錯誤","index.php");
die();
}
}
else
{
$sql="update shopcart set ptl= ptl+1,psum = psum+price where ID='".$ex1['ID']."'";
mysql_query($sql);
$this-alter("更新數(shù)量成功","index.php");
}
}
//更新購物車的單個產品的數(shù)量;
function updata_cart($cid,$ptl,$pid)
{
if($ptl=100||$ptl=0)
{
$this-alter('產品數(shù)量不對!','index.php');
die();
}
$sql="select * from shopcart where ID='".$cid."' and pid='".$pid."'";
$ok=mysql_query($sql);
if(!ok) { alter("參數(shù)發(fā)生錯誤","index.php");}
else
{
$sql="update shopcart set ptl='".$ptl."',psum=price * '".$ptl."' where ID='".$cid."' and pid='".$pid."'";
$ok=mysql_query($sql);
if(!ok) { $this-alter("更新失敗","index.php");}
else { $this-alter("更新成功","index.php");}
}
}
function del_cart($cid,$pid,$usr)
{
$sql="delete from shopcart where usr='".$usr."' and ID='".$cid."' and pid='".$pid."'";
$ok=mysql_query($sql);
if(!$ok) {$this-alter("刪除失敗","index.php");}
else {$this-alter("刪除成功","index.php");}
}
function empty_cart($usr)
{
$sql="delete from shopcart where usr='".$usr."'";
mysql_query($sql) or die(mysql_error);
}
function get_cart($usr)
{
$sql="select * from shopcart where usr='".$usr."'";
$ok=mysql_query($sql);
return $ok;
}
}
$my = new Cart("localhost","root","root","mybbs");
//$my-add_cart(45,3,"茶幾系列");
//$my-updata_cart(13,13,8);
//$my-del_cart(12,5,'Guest');
//$my-empty_cart('Guest');
$ok=$my-get_cart('Admin');
echo "usr pid pname ptl price pcid psum payment ptime brhrbr";
while($rs=mysql_fetch_array($ok))
{
echo $rs[1]."-".$rs[2]."-".$rs[3]."-".$rs[4]."-".$rs[5]."-".$rs[6]."-".$rs[7]."-".$rs[8]."-".$rs[9]."br";
}
?
、、、、、、、、、、、、、、、、、SQL、、、、、、、、、、、、、、
CREATE TABLE IF NOT EXISTS `shopcart` (
`ID` int(10) NOT NULL auto_increment,
`usr` varchar(50) NOT NULL,
`pid` int(5) NOT NULL,
`pname` varchar(100) NOT NULL,
`ptl` int(3) NOT NULL,
`price` decimal(50,2) NOT NULL default '0.00',
`pcid` varchar(100) NOT NULL,
`psum` decimal(50,2) NOT NULL default '0.00',
`payment` tinyint(1) NOT NULL,
`ptime` timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
)
product 里面用的ID CLASS1是
`ID` int(6) NOT NULL auto_increment,
`Class1` varchar(20) NOT NULL,
`Price` int(6) NOT NULL,
sql語句是select * from 你先打印出來查詢的數(shù)據(jù),再輸出想要的數(shù)據(jù)在頁面就行了
我來解答一下你的疑惑
買了兩個產品。那就是執(zhí)行了兩次
insert
into
temp_table
(uid,productid,pnum,poneprice,ptotalprice)
如果
productid相同則,
pnum
=
pnum+1;
ptotalprice
=
pnum*poneprice
假設前提是
當前兩條記錄的產品不同,那么購物車列表則是循環(huán)讀取temp_table列出現(xiàn)有符合條件之產品,數(shù)量,價格。
修改2個產品數(shù)量的時候,
提交后,同樣的文本框pnum為一個數(shù)組,productid為一個數(shù)組
獲取pnum,productid,并且用
split分析后,
分別update
update
temp_table
set
pnum='".$pnum[0]."',ptotalprice='..省略.'
where
uid=自己的uid
and
productid='".$productid[0]."'
注意,這里數(shù)組下標要對應好,你可以用個循環(huán)。
最后,當訂單下好之后,要把臨時表的數(shù)據(jù)轉移到正式表中,并且清理掉當前這個用戶臨時表的內容即可。
網頁標題:php數(shù)據(jù)庫購物車下載,php加入購物車怎樣實現(xiàn)
文章位置:http://chinadenli.net/article28/dsihejp.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供企業(yè)網站制作、網站制作、、企業(yè)建站、電子商務、自適應網站
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)