本文目錄一覽:
- 1、【高分】急求用php寫的購物車代碼!!!!!(十萬火急)如果您提供的好用還有加分!!!
- 2、php 實現 購物車 提交到 訂單的數據庫
- 3、php是怎麼實現購物車的,SESSION 和cookie 在哪裡會用到
- 4、PHP怎麼做購物車?
- 5、php購物車怎麼實現
- 6、購物車滿100減2元,200減5元 300減7元,php這個代碼怎麼寫,腦殼都想破了。
【高分】急求用php寫的購物車代碼!!!!!(十萬火急)如果您提供的好用還有加分!!!
我也要弄一個這種購物車,
我去寫個,貼出來,【嘿嘿,今天上午新寫的】。
我懶得新建數據庫,用的是我的數據庫。
你按照我的改一下就能用了
本人水平有限,高手請指正。
你,大,爺的,雖然不咋地,保證能用
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
經過調試,
//$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.””;
}
//增加購物;三個參數:pid:產品ID,ptl:產品數量,pcid:產品類別
//查詢數據庫,是否存在此人在本日內訂過本產品
//如果訂過,那麼數量累加,否則插入一個數據庫行
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(“不存在的商品,或者參數錯誤”,”index.php”);
die();
}
}
else
{
$sql=”update shopcart set ptl= ptl+1,psum = psum+price where ID='”.$ex1[‘ID’].”‘”;
mysql_query($sql);
$this-alter(“更新數量成功”,”index.php”);
}
}
//更新購物車的單個產品的數量;
function updata_cart($cid,$ptl,$pid)
{
if($ptl=100||$ptl=0)
{
$this-alter(‘產品數量不對!’,’index.php’);
die();
}
$sql=”select * from shopcart where ID='”.$cid.”‘ and pid='”.$pid.”‘”;
$ok=mysql_query($sql);
if(!ok) { alter(“參數發生錯誤”,”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,
php 實現 購物車 提交到 訂單的數據庫
用戶登陸後系統應該會分配一個session : id,這個id應該是改用於的用戶表id。然後再做一個購物車的表,裡面可以只考慮存商品id、用戶id等關鍵信息;
當用戶提交購買的時候再做相關的操作,具體可以參考淘寶、天貓的購物車已經後續付款效果。
php是怎麼實現購物車的,SESSION 和cookie 在哪裡會用到
購物車就是用session來實現的。session和cookie的不同主要在於前者保存在服務器端,而後者保存在客戶端電腦上。比如用戶登錄,瀏覽器經常提示你是否保存登錄信息,如果保存了,就新建了一個cookie在你的電腦上。而像購物車之類,或者用戶登錄狀態,則是用session。
PHP怎麼做購物車?
購物車有兩種實現方式,一種是保存在數據庫,另外一種是session
保存在數據庫的不會以為關閉瀏覽器而消失,session會因為關閉瀏覽器就沒有了。
原理是把每個商品的信息存到一個數組裡面,然後以這個商品的id作為鍵值,然後吧數組存到session裡面就行,
如果是存入數據庫的話,就用關聯數據存一下就行的
php購物車怎麼實現
可以使用 session 或者 redis 來保存購物車信息。
具體代碼這裡不可能貼出來的。建議你下載一個開源的商城系統,看看別人是怎麼寫的。
購物車滿100減2元,200減5元 300減7元,php這個代碼怎麼寫,腦殼都想破了。
$count_price = 多少錢;//購物車總金額
if($count_price =100 $count_price200)
{
$count_price = $count_price-2;
}else if($count_price=200 $count_price300){
$count_price = $count_price -5;
}else if($count_price=300){
$count_price = $count_price – 7;
}
原創文章,作者:HXTW,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/149655.html