當前位置:首頁 » 網購平台 » php購物車的原理代碼
擴展閱讀
寧波奧德賽優惠價格 2021-03-15 14:26:02
丹尼斯購物卡能掛失么 2021-03-15 14:25:58
淘寶購物指紋驗證失敗 2021-03-15 14:24:44

php購物車的原理代碼

發布時間: 2021-02-02 04:16:01

1. php session實現購物車的原理

session_start;
然後原理就是,建立一個數組,$_SESSION['wupin'][]=選中的物品的ID,然後就可以了,如果用戶取消,就把他選擇取消的那個ID的鍵從數組里刪除就行。

2. 購物車代碼怎麼寫php

寫就行了,跟普通的一樣,不就是資料庫的增刪改查嗎

3. php 購物車代碼~呢

<?php 
class Shopcar 

//商品列表 
public $proctList=array(); 
/** 

* @param unknown_type $proct 傳進來的商品 
* @return true 購物車裡面沒有該商品 
*/ 
public function checkProct($proct) 

for($i=0;$i<count($this->proctList);$i++ ) 

if($this->proctList[$i]['name']==$proct['name']) 
return $i; 

return -1; 

//添加到購物車 
public function add($proct) 

$i=$this->checkProct($proct); 
if($i==-1) 
array_push($this->proctList,$proct); 
else 
$this->proctList[$i]['num']+=$proct['num']; 

//刪除 
public function delete($proct) 

$i=$this->checkProct($proct); 
if($i!=-1) 
array_splice($this->proctList,$i,1); 

//返回所有的商品的信息 
public function show() 

return $this->proctList; 


你可以去後盾人平台看看,裡面的東西不錯

4. 【高分】急求用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 proct 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 <br><hr><br>";
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`)
)

proct 裡面用的ID CLASS1是

`ID` int(6) NOT NULL auto_increment,
`Class1` varchar(20) NOT NULL,
`Price` int(6) NOT NULL,

5. PHP 高手 請進來看下這段購物車代碼

$sql="SELECT name,price FROM proct WHERE id='$id'";
$id是變數,php中雖然雙引號和單引號都能表示字元串,但是不同的內是,單引號不能解析變數,也就容是說'$aaaa'表示的就是字元串$aaaa,而不會解析字元串!
改為:
$sql="SELECT name,price FROM proct WHERE id=$id";

6. 求PHP里的TP5的購物車代碼

可以參考如下代碼

<?php
classCartextendsThink{
//當前購物車名
public$sessionName;
//購物車總價格
public$totalPrice
publicfunction__construct($sessionName)
{
$this->sessionName=$sessionName;
if(!isset($_SESSION[$this->sessionName]))
{
$_SESSION[$this->sessionName]="";
}
}

//獲取購物車的信息
publicfunctiongetCart(){
$cur_cart_array=$_SESSION[$this->sessionName];
return$cur_cart_array;
}

//獲取購物車商品清單
publicfunctiongetCartList()
{
$cur_cart_array=$_SESSION[$this->sessionName];
if($cur_cart_array!="")
{
$mode_goods_data=M("goods_data");
$len=count($cur_cart_array);
for($i=0;$i<$len;$i++)
{
$goodsid=$cur_cart_array[$i]["id"];
$num=$cur_cart_array[$i]["num"];
$query="select(selectsfilenamefromgoods_picwheregoodsid=a.goodsidorderbysnodesclimit0,1)assfilename,b.clsnameasclsname,a.goodsidasgoodsid,a.goodsnameasgoodsname,a.PriceasPrice,a._dataaleftjoingoods_clsbona.Clsid=b.clsidwherea.goodsid=$goodsid";
$list=$mode_goods_data->query($query);
$list[0]["qty"]=$num;
$list[0]["amount"]=$num*$list[0]["Price"];
$cartList[$i]=$list[0];
$totalPrice+=$list[0]["amount"];
}
//返回商品總價格
$this->totalPrice=$totalPrice;
return$cartList;
}
}

//加入購物車,購物車的商品id和購物車的商品數量
publicfunctionaddcart($goods_id,$goods_num){
$cur_cart_array=$_SESSION[$this->sessionName];
if($cur_cart_array=="")
{
$cart_info[0]["id"]=$goods_id;//商品id保存到二維數組中
$cart_info[0]["num"]=$goods_num;//商品數量保存到二維數組中
$_SESSION[$this->sessionName]=$cart_info;
}
else
{
//返回數組鍵名倒序取最大
$ar_keys=array_keys($cur_cart_array);
$len=count($ar_keys);
$max_array_keyid=$ar_keys[$len-1]+1;
//遍歷當前的購物車數組
//遍歷每個商品信息數組的0值,如果鍵值為0且貨號相同則購物車該商品已經添加
$is_exist=$this->isexist($goods_id,$goods_num,$cur_cart_array);
if($is_exist==false)
{
$cur_cart_array[$max_array_keyid]["id"]=$goods_id;
$cur_cart_array[$max_array_keyid]["num"]=$goods_num;
$_SESSION[$this->sessionName]=$cur_cart_array;
}
else
{
$arr_exist=explode("/",$is_exist);
$id=$arr_exist[0];
$num=$arr_exist[1];
$cur_cart_array[$id]["num"]=$num;
$_SESSION[$this->sessionName]=$cur_cart_array;
}
}
}

//判斷購物車是否存在相同商品
publicfunctionisexist($id,$num,$array)
{
$isexist=false;
foreach($arrayas$key1=>$value)
{
foreach($valueas$key=>$arrayid)
{
if($key=="id"&&$arrayid==$id)
{
$num=$value["num"]+$num;
$isexist=$key1."/".$num;
}
}
}
return$isexist;
}
thinkphp開發使得我們比較容易的去進行了

//從購物車刪除
publicfunctiondelcart($goods_array_id){
//回復序列化的數組
$cur_goods_array=$_SESSION[$this->sessionName];
//刪除該商品在數組中的位置
unset($cur_goods_array[$goods_array_id]);
$_SESSION[$this->sessionName]=$cur_cart_array;
//使數組序列化完整的保存到cookie中
}

//清空購物車
publicfunctionemptycart(){
$_SESSION[$this->sessionName]="";
}

//修改購物車貨品數量
publicfunctionupdate_cart($up_id,$up_num){
//回復序列化的數組
$cur_goods_array=$_SESSION[$this->sessionName];
$cur_goods_array[$up_id]["num"]=$up_num;
$_SESSION[$this->sessionName]=$cur_cart_array;
}
}
?>

7. 購物車源碼思路PHP

首先確定購物車中商品結構(都有哪些欄位)。
比如:商品ID,商品名稱,數量,單價回等等。
然後,將這些答結構定義在數組里
array(
商品ID1=>array('name'=>'商品','num'=>1,'price'=>100),
商品ID2=>array('name'=>'商品','num'=>1,'price'=>100),
)
然後把這個大的數組保存在SESSION里。就可以了。
PHP有很多數組操作函數用起來也很方便。

8. php關於用資料庫作為購物車的原理

我來解答一下你復的疑惑 買了兩個產品制。那就是執行了兩次 insert into temp_table (uid,proctid,pnum,poneprice,ptotalprice)如果 proctid相同則, pnum = pnum+1; ptotalprice = pnum*poneprice 假設前提是 當前兩條記錄的產品不同,那麼購物車列表則是循環讀取temp_table列出現有符合條件之產品,數量,價格。 修改2個產品數量的時候, 提交後,同樣的文本框pnum為一個數組,proctid為一個數組 獲取pnum,proctid,並且用 split分析後, 分別update update temp_table set pnum='".$pnum[0]."',ptotalprice='..省略.' where uid=自己的uid and proctid='".$proctid[0]."'注意,這里數組下標要對應好,你可以用個循環。 最後,當訂單下好之後,要把臨時表的數據轉移到正式表中,並且清理掉當前這個用戶臨時表的內容即可。

9. php 購物車代碼一般是用什麼方法實現

將你的session,看成抄一個數組。
$_SESSION['user_id']=array{
['商品襲id']=array{
'價格'=>501,
},

['商品id']=array{
'價格'=>502,
},

['商品id']=array{
'價格'=>503,
}
}

以上保存了3件商品的信息,如果用戶id為20的人,期間修改了商品的信息,就根據user_id與商品id,去修改你的這個數組,最後生成訂單的時候,insert入庫。

10. 求一個php session 購物車類代碼

最好還是建一個專門的購物車表,不要用session存儲,這樣雖然方便,但是會員在其他地方登錄或者換個時間購物車內的東西就不存在了,這樣太不好啊。