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

php購物車示例

發布時間: 2021-02-15 14:20:24

A. 如何用html php製作購物車

網上有好多教程你隨便搜一下就有了

B. PHP怎麼做購物車

購物車復有兩種實現方式,一種是制保存在資料庫,另外一種是session

保存在資料庫的不會以為關閉瀏覽器而消失,session會因為關閉瀏覽器就沒有了。

原理是把每個商品的信息存到一個數組裡面,然後以這個商品的id作為鍵值,然後吧數組存到session裡面就行,

如果是存入資料庫的話,就用關聯數據存一下就行的

C. 哪位高手給一個完整的php+MYSQL的購物車源代碼

我也正學呢我要的是PHP+mysql帶資料庫表的,但是沒找到完整免費下載的

D. 一個最簡單的用PHP實現購物車功能,請高手指點

這個感覺用cookie就好了啦
你一步步的跟蹤一下,看看是哪兒沒拿到值,可以在foreach之前把session的數組轉存到一個變數里

E. 跪求 PHP+Mysql實現購物車功能

PHP+Mysql實現購物車功能1.伺服器端編程語言是需要購物車代碼的,沒有伺服器代碼闡述!

F. php購物車展示是根據那個ID顯示出來的

在用戶表的加個購物車欄位,記錄下商品的id,剩下的都會了吧

G. 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; 


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

H. 購物車源碼思路PHP

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

I. thinkphp做的session購物車,求詳細代碼。有完整例子的可以發下。非常感謝

<?php


classCartTool{
privatestatic$ins=null;
private$items=array();

finalprotectedfunction__construct(){
}

finalprotectedfunction__clone(){
}

//獲取實例
protectedstaticfunctiongetIns(){
if(!(self::$insinstanceofself)){
self::$ins=newself();
}

returnself::$ins;
}


//把購物車的單例對象放到session里
publicstaticfunctiongetCart(){
if(!isset($_SESSION['cart'])||!($_SESSION['cart']instanceofself)){
$_SESSION['cart']=self::getIns();
}

return$_SESSION['cart'];
}


/*
添加商品
paramint$id商品主鍵
paramstring$name商品名稱
paramfloat$ 商品價格
paramint$num購物數量
*/
publicfunctionaddItem($id,$name,$price,$brand,$thumb,$num=1){

if($this->hasItem($id)){//如果該商品已經存在,則直接加其數量
$this->incNum($id,$num);
return;
}

$item=array();
$item['id']=$id;
$item['name']=$name;
$item['price']=$price;
$item['brand']=$brand;
$item['thumb']=$thumb;
$item['num']=$num;

$this->items[$id]=$item;

return$this->items[$id];


}


/*
修改購物車中的商品數量
paramint$id商品主鍵
paramint$num某個商品修改後的數量,即直接把某商品的數量改為$num
*/
publicfunctionmodNum($id,$num=1){
if(!$this->hasItem($id)){
returnfalse;
}

$this->items[$id]['num']=$num;

}


/*
商品數量增加1
*/
publicfunctionincNum($id,$num=1){
if($this->hasItem($id)){
$this->items[$id]['num']+=$num;
}
}


/*
商品數量減少1
*/
publicfunctiondecNum($id,$num=1){
if($this->hasItem($id)){
$this->items[$id]['num']-=$num;
}

//如果減少後,數量為0了,則把這個商品從購物車刪掉
if($this->items[$id]['num']<1){
$this->delItem($id);
}
}


/*
判斷某商品是否存在
*/
publicfunctionhasItem($id){
returnarray_key_exists($id,$this->items);
}


/*
刪除商品
*/
publicfunctiondelItem($id){
unset($this->items[$id]);
}


/*
查詢購物車中商品的種類
*/
publicfunctiongetCnt(){
returncount($this->items);
}


/*
查詢購物車中商品的個數
*/
publicfunctiongetNum(){
if($this->getCnt()==0){
return0;
}

$sum=0;

foreach($this->itemsas$item){
$sum+=$item['num'];
}

return$sum;
}


/*
查詢購物車中商品的總金額
*/
publicfunctiongetPrice(){
if($this->getCnt()==0){
return0;
}

$price=0.0;

foreach($this->itemsas$item){
$price+=$item['num']*$item['price'];
}

return$price;
}/*
返回購物車中的所有商品
*/

publicfunctionall(){
return$this->items;
}

/*
清空購物車
*/
publicfunctionclear(){
$this->items=array();
}
}




以上是類文件,需要引入的。



$cart=CartTool::getCart();
$car_goods_list=$cart->all();//獲取商品列表

$goods_num=$cart->getNum();//商品個數

$goods_sum_price=$cart->getPrice();//總價格

$cart->addItem($goods_id,$goods_list['goods_name'],$goods_list['shop_price'],$goods_list['brand'],$goods_list['goods_thumb'],$num);//增加一件商品到購物車

$cart->clear();//清空購物車

J. 【高分】急求用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,