當前位置:首頁 » 編程語言 » php購物車功能

php購物車功能

發布時間: 2023-05-09 18:23:57

1. phpcms支持購物車功能嗎

PHPCMS是開源程序
程序本身沒神仿有購物車和類似功能
需要二次開發購物車功能或者直接購買世轎別人開發的PHPCMS購搜瞎肆物車插件安裝到PHPCMS上

2. 求php增加購物車數量的加減,

偽代碼:
前端:在你頁面上數量那欄增加

<div class="quantity-form"><a href="javascript:void(0);" clstag="cart_num_down" class="decrement disabled" id="decrement_8888_526830_1_1">-</a>
<input autocomplete="off" type="text" class="itxt" value="1" id="changeQuantity_8888_526830_1_1_0" minnum="1">
<a href="javascript:void(0);" clstag="cart_num_up" class="increment" id="increment_8888_526830_1_1_0">+</a>
</div>
你的購物車是個循環列表,可以去得到,每個商品的信息:id是商品
function cart_num_up(id,uid){
$.ajax({
type: "POST",
url: "CART_num.PHP",
data: {id:id, num:-1,uid:uid},
dataType: "json",
success: function(data){
if(data.status==1){

});
});

}
function cart_num_down(id,uid){
$.ajax({
type: "POST",
url: "CART_num.PHP",
data: {id:id, num:1,uid:uid},
dataType: "json",
success: function(data){
if(data.status==1){

});
});
}
點一次 加或減按鈕,觸發js向後端發起ajax請求:返回的是增加成功和失敗狀態和剩餘數量;
CART_num.PHP
$id = (int)$_POST['id'];
$num = (int)$_POST['num'];
$uid =(int)$_POST['uid']; //有封裝獲取post或get函數更好
//查庫看庫存
$sort = get_kucun_num();
//查看購物車目前數量
$cart_num = get_cart_num(uid,id);
//判斷庫存
if($sort >$cart_num){
}else{
}

if($num<0){
//減法 $sql =「update cart set cart_num = cart_num-1 where uid =uid and id =id 」
if(($cart_num-1)>1){
}else{
}){
//加法 $sql =「update cart set cart_num = cart_num+1 where uid =uid and id =id 」
}else{
//不正確的請求
}

3. 求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;
}
}
?>

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中使用什麼方法可以讓購物車頁面刷新時數量不增加

這要看你是怎麼做的購物車?為何刷新時會增加數量呢?
一般我的做法是:
1.用ajax的方式實現.點擊加入購物車.當然頁面都不動.而是後台處理.
2.購物車的內容是根據已經保存的數據來顯示的.和刷新根本沒關系.

6. 設計一個購物車,包含html和php兩個文件,html讓客戶輸入個人信息以及顯示商品信息。php將訂單信息返回給

訂單表,客戶信息表,商品信息表,購物車表存放客戶信息表,商品信息表的主鍵id,用來關聯,並且建立一個數量的欄位,用來計算總價。客戶錄入表單去提交頁面添加判斷然後跳轉購物頁,添加購物車的話直接基於數量和兩個主鍵id去添加,下單的話基於時間戳生成訂單編號和客戶編號存放在訂單表中,記得主外鍵關聯

7. php 購物車。 在一產品列表上,任選多個產品,然後直接提交到購物車上。請問這個代碼如何寫,需要具體代碼

可以用數組提交
表單中 這么寫 <input name="a[]" type="checkbox" /> <input name="id[]" type="hidden" /><input name="num[]" />
關鍵是name=" "寫成數組,具體寫法看你的具體情況
後台
//循環處理 產品信息

foreach($a as $k => $v)
{
$now_id = $_POST['id'][$k];
$now_num = $_POST['num'][$k];
//下面你自己寫

}

8. php 實現 購物車 提交到 訂單的資料庫

用戶登陸後系統應該會分配一個session : id,這個id應該是改用於的用戶表id。然後再做一個購物車的表,裡面可以只考慮存商品id、用戶id等關鍵信息;
當用戶提交購買的時候再做相關的操作,具體可以參考淘寶、天貓的購物車已經後續付款效果。

9. PHP如何用Cookie做購物車

我有個session的例子你看看吧!!!你把session換成cookie就可以絕賀了!!!

//增加購物車物品
if($name==$_SESSION["car"][$name]["name"])
{
$_SESSION["car"][$name]["count"]++;
}
else{
$_SESSION["car"][$name]["name"]=$name;
$_SESSION["car"][$name]["maney"]=$maney;
$_SESSION["car"][$name]["count"]=1;
}

//銷宏橋刪除物品
if( $_SESSION["car"][$name]["count"]>1 && $dele=='1')
{
$_SESSION["car"][$a]["count"虧猛]--;
}

10. 大神們,出來吧。。PHP中購物車的功能實現,存放在資料庫中是否會造成數據表數據過多,讀取緩慢。

購物車新建一個表,外鍵到用戶ID,這樣可以有效控制冗餘。

熱點內容
資料庫的根本目標 發布:2025-07-18 21:37:50 瀏覽:937
壓縮機的流速 發布:2025-07-18 21:37:40 瀏覽:406
三星怎麼取消手機密碼 發布:2025-07-18 21:33:50 瀏覽:629
安卓手機耳機如何彈窗顯示電量 發布:2025-07-18 21:20:53 瀏覽:59
雲伺服器搭建需要什麼工具 發布:2025-07-18 20:51:08 瀏覽:322
如何提高手機緩存速度 發布:2025-07-18 20:24:48 瀏覽:237
vba讀取資料庫數據 發布:2025-07-18 20:24:48 瀏覽:608
shell解壓zip 發布:2025-07-18 20:20:36 瀏覽:859
安卓泰拉瑞亞去哪裡買 發布:2025-07-18 20:01:05 瀏覽:694
flash編譯器 發布:2025-07-18 19:49:38 瀏覽:487