導航:首頁 > 編程語言 > php購物車

php購物車

發布時間:2022-01-17 00:00:11

1. 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];
//下面你自己寫

}

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如何用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"]--;
}

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

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

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

6. 購物車源碼思路PHP

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

7. php購物車點擊加減的時候在不刷新頁面的時候,價格相對增加或減少該怎麼實現

頁面只是一個效果,可以通過js直接計算出價格後,再替換原來的價格『價格對應元素的對象.text('新的價格')』顯示出來;
如果結算方式比較復雜,建議通過Ajax傳值到後台進行計算後,再替換原來的價格『價格對應元素的對象.text('新的價格')』顯示出來;
obj.prev.val(++num);
之後加入下行(如果class='number'的元素是價格顯示區域的話)
obj.closest('.btn-numbox').find('.number').text(num*單價);

8. 我用PHP做的購物車,為什麼每次往購物車中添加不同商品 ,購物車中確顯示的同一件商品啊

這個需要看你的購物車結構。
一般一個屬性的商品是在一條數據裡面,多次加入只是更改數量
不同屬性的商品應該分多條數據保存。最後購物車統計的是總的數量

9. php購物車添加和減少數量以及計算總金額

增減裡面的get的是 BID ,你傳值用的是 id ,所以session裡面獲取不到$I_ID的值 ,默認變成空字元串,所以你後面的增減操作都是對中間那個沒有id的進行操作 .

總量遍歷數組,把單價和數量相乘 然後相加 .

10. PHP怎麼做購物車

用戶操作的數據暫存在cookie或者session里,生成訂單的時候從以上兩者讀取相關數據,結合資料庫查詢生成訂單. 好像沒什麼困難的原理啊.

閱讀全文

與php購物車相關的資料

熱點內容
xp點擊文件夾選項沒反應 瀏覽:537
蘋果不顯示桌面的app怎麼刪除 瀏覽:864
安卓手機怎麼換國際服 瀏覽:414
神獸領域安卓怎麼下載 瀏覽:250
單片機交通燈ad原理圖 瀏覽:413
多功能解壓磁鐵筆 瀏覽:80
少兒編程火箭升空 瀏覽:401
蘭斯10游戲解壓碼 瀏覽:42
手機proxy伺服器地址 瀏覽:449
吉他清音壓縮 瀏覽:301
簡歷模板程序員 瀏覽:882
螺桿壓縮機虛標型號 瀏覽:953
idea開發項目伺服器ip地址 瀏覽:125
串口伺服器出現亂碼怎麼解決 瀏覽:950
命令按鈕的default 瀏覽:161
戰網如何登錄其他伺服器 瀏覽:990
中國銀行app如何關閉簡訊 瀏覽:493
nx120編程技巧 瀏覽:722
手機也能使用源碼公式 瀏覽:918
怎樣把壓縮的文件下載 瀏覽:335