導航:首頁 > 編程語言 > php加入購物車代碼

php加入購物車代碼

發布時間:2024-04-26 14:41:39

Ⅰ 怎樣用php編個購物車的程序

簡單說了,可以用Dreamweaver做網頁開發平台,PHPnow打包套件作為後台,包括Apache(作為伺服器)+PHP(作為PHP庫資源)+MySQL(作為資料庫環境)。要在資料庫的添加表單等等操作,在PhpAdmin下登錄進入MySQL。

編輯網頁的內容會在一個叫做htdocs的文件夾內,編輯資料庫的內容會在一個叫data的文件夾內。

用Dreamweaver時要設置PHPnow的路徑作為站點。

Ⅱ php中的類有什麼用和自定義函數有什麼區別

請看PHP手冊18章,裡面的內容你看了就明白。

第18章類與對象(PHP4)
目錄

繼承
構造函數
范圍解析操作符(::)
parent
序列化對象-會話中的對象
魔術函數__sleep和__wakeup
構造函數中的引用
對象的比較



類是變數與作用於這些變數的函數的集合。使用下面的語法定義一個類:

<?php
classCart{
var$items;//購物車中的物品

//將$num個$artnr物品加入購物車

functionadd_item($artnr,$num){
$this->items[$artnr]+=$num;
}

//將$num個$artnr物品從購物車中取出

functionremove_item($artnr,$num){
if($this->items[$artnr]>$num){
$this->items[$artnr]-=$num;
returntrue;
}elseif($this->items[$artnr]==$num){
unset($this->items[$artnr]);
returntrue;
}else{
returnfalse;
}
}
}
?>

上面的例子定義了一個Cart類,這個類由購物車中的商品構成的數組和兩個用於從購物車中添加和刪除商品的函數組成。

警告
不能將一個類的定義分割到多個文件中。也不能將一個類的定義分割到多個PHP塊中,除非該分割是在一個方法聲明內部。以下用法將不起作用:

<?php
classtest{
?>
<?php
functiontest(){
print'OK';
}
}
?>

但是以下用法是可以的:

<?php
classtest{
functiontest(){
?>
<?php
print'OK';
}
}
?>

以下警告僅用於PHP4。

小心
名稱stdClass已經被Zend使用並保留。不能在PHP代碼中定義名為stdClass的類。

小心
函數名__sleep和__wakeup在PHP類中是魔術函數。除非想要與之聯系的魔術功能,否則在任何類中都不能以此命名函數。

小心
PHP將所有以__開頭的函數名保留為魔術函數。除非想要使用一些見於文檔中的魔術功能,否則建議不要在PHP中將函數名以__開頭。

在PHP4中,var變數的值只能初始化為常量。用非常量值初始化變數,需要一個初始化函數,該函數在對象被創建時自動被調用。這樣一個函數被稱之為構造函數(見下面)。

<?php
/*PHP4中不能這樣用*/
classCart{
var$todays_date=date("Y-m-d");
var$name=$firstname;
var$owner='Fred'.'Jones';
/*不過包含有常量的數組可以*/
var$items=array("VCR","TV");
}

/*應該這樣進行*/
classCart{
var$todays_date;
var$name;
var$owner;
var$items=array("VCR","TV");
functionCart(){
$this->todays_date=date("Y-m-d");
$this->name=$GLOBALS['firstname'];
/*etc...*/
}
}
?>
類也是一種類型,就是說,它們是實際變數的藍圖。必須用new運算符來創建相應類型的變數。

<?php
$cart=newCart;
$cart->add_item("10",1);

$another_cart=newCart;
$another_cart->add_item("0815",3);
?>
上述代碼創建了兩個Cart類的對象$cart和$another_cart,對象$cart的方法add_item()被調用時,添加了1件10號商品。對於對象$another_cart,3件0815號商品被添加到購物車中。

$cart和$another_cart都有方法add_item(),remove_item()和一個items變數。它們都是明顯的函數和變數。可以把它們當作文件系統中的某些類似目錄的東西來考慮。在文件系統中,可以擁有兩個不同的README.TXT文件,只要不在相同的目錄中。正如從為了根目錄訪問每個文件需要輸入該文件的完整的路徑名一樣,必須指定需要調用的函數的完整名稱:在PHP術語中,根目錄將是全局名字空間,路徑名符號將是->。因而,名稱$cart->items和$another_cart->items命名了兩個不同的變數。注意變數名為$cart->items,不是$cart->$items,那是因為在PHP中一個變數名只有一個單獨的美元符號。

<?php
//正確,只有一個$
$cart->items=array("10"=>1);

//不正確,因為$cart->$items變成了$cart->""
$cart->$items=array("10"=>1);

//正確,但可能不是想要的結果:
//$cart->$myvar變成了$cart->items
$myvar='items';
$cart->$myvar=array("10"=>1);
?>
在定義類的時候,無法得知將使什麼名字的對象來訪問:在編寫Cart類時,並不知道之後對象的名稱將會命名為$cart或者$another_cart。因而你不能在類中使用$cart->items。然而為了類定義的內部訪問自身的函數和變數,可以使用偽變數$this來達到這個目的。$this變數可以理解為「我自己的」或者「當前對象」。因而'$this->items[$artnr]+=$num'可以理解為「我自己的物品數組的$artnr計數器加$num」或者「在當前對象的物品數組的$artnr計數器加$num」。

注意:偽變數$this通常未定義,如果其所在的方法是被靜態調用的話。但這不是個嚴格規定:如果一個方法被從另一個對象內靜態調用的話,則$this會被定義。此時$this的值是那個發出調用的對象。用下例演示:

<?php
classA
{
functionfoo()
{
if(isset($this)){
echo'$thisisdefined(';
echoget_class($this);
echo")\n";
}else{
echo"\$thisisnotdefined.\n";
}
}
}

classB
{
functionbar()
{
A::foo();
}
}

$a=newA();
$a->foo();
A::foo();
$b=newB();
$b->bar();
B::bar();
?>
上例將輸出:

$thisisdefined(a)
$thisisnotdefined.
$thisisdefined(b)
$thisisnotdefined.注意:有一些不錯的函數用來處理類和對象。應該關注一下類/對象函數。

Ⅲ 求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{
//不正確的請求
}

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

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

Ⅳ 如何用html css javascript php製作購物車

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="---.css">
<script src="---.js"></script>
</head>
<body>
<div class="container">
<div class="shop">
<div class="header">
<input type="checkbox" class="shop-checkbox">
<span class="shop-icon"></span>
<span class="shop-name">---</span>
<span class="wangwang-icon"></span>
</div>
<div class="items">
<div class="item">
<div><input class="item-checkbox" type="checkbox" name="" id=""></div>
<div>
<span class="item-img"></span>
</div>
<div class="item-name">
<div>----</div>
<div class="promotion-icons"><span></span><span></span><span></span></div>
</div>
<div class="sku">---</div>
<div class="price">
<div class="price-1">---</div>
<div class="price-2" data-price="---">----</div>
</div>
<div class="num-control">
<span class="num-minus">-</span>
<input class="num" type="text" value="1">
<span class="num-plus">+</span>
</div>
<div class="item-price-total">
<span>¥50.13</span>
</div>
<div class="operation">
<div>---</div>
<div>---</div>
</div>
</div>
</div>
</div>

<div class="shop">
<div class="header">
<input type="checkbox" class="shop-checkbox">
<span class="shop-icon"></span>
<span class="shop-name">---</span>
<span class="wangwang-icon"></span>
</div>
<div class="items">
<div class="item">
<div><input class="item-checkbox" type="checkbox" name="" id=""></div>
<div>
<span class="item-img"></span>
</div>
<div class="item-name">
<div>----</div>
<div class="promotion-icons"><span></span><span></span><span></span></div>
</div>
<div class="sku">---</div>
<div class="price">
<div class="price-1">---</div>
<div class="price-2" data-price="---">---</div>
</div>
<div class="num-control">
<span class="num-minus">-</span>
<input class="num" type="text" value="1">
<span class="num-plus">+</span>
</div>
<div class="item-price-total">
<span>¥9.90</span>
</div>
<div class="operation">
<div>移入收藏夾</div>
<div>刪除</div>
</div>
</div>

<div class="item">
<div><input class="item-checkbox" type="checkbox" name="" id=""></div>
<div>
<span class="item-img"></span>
</div>
<div class="item-name">
<div>---</div>
<div class="promotion-icons"><span></span><span></span><span></span></div>
</div>
<div class="sku">---</div>
<div class="price">
<div class="price-1">---</div>
<div class="price-2" data-price="---">---</div>
</div>
<div class="num-control">
<span class="num-minus">-</span>
<input class="num" type="text" value="1">
<span class="num-plus">+</span>
</div>
<div class="item-price-total">
<span>¥19.9</span>
</div>
<div class="operation">
<div>移入收藏夾</div>
<div>刪除</div>
</div>
</div>
</div>

</body>
</html>

.container{
width: 1000px;
margin-left: auto;
margin-right: auto;
color: #444444;
}
.header{
margin: 8px;
}
.shop-icon, .wangwang-icon, .promotion-icons span{
display:inline-block;
width: 15px;
height: 15px;
background-color: rgb(117,192,241);
}
.promotion-icons span{
margin-right: 4px;
}
.items{
border: 1px solid #ebe9e9;
}
.item{
display: flex;
margin: 8px;
}
.item-img{
width:100px;
height: 100px;
display: inline-block;
background-color: aquamarine;
margin-left: 6px;
margin-right: 6px;
}
.item-name
{
display: flex;
flex-direction: column;
justify-content: space-between;
}
.sku, .price,.item-price-total,.operation,.num-control{
margin-left: 18px;
}
.num{
width: 18px;
height: 15px;
}
.num-control{
display: flex;
align-items: baseline;
}
.num-minus,.num-plus{
width: 18px;
height: 22px;
display: inline-block;
background-color: #ebe9e9;
}
.checkout{
display: flex;
justify-content: space-between;
}
.shop{
margin-bottom: 20px;
margin-top: 30px;
}
.goods,.freight,.checkout-button{
margin-left: 15px;
}
.price-1{
text-decoration: line-through;
color: gray;
}
.check-num ,.total-price{
font-size: large;
color: red;
margin-left: 3px;
margin-right: 3px;
}
.checkout-button{
height: 30px;
width: 50px;
background-color: beige;
}
.checkout{
margin-top: 15px;
}
.item-name{
width: 25%;
}
.item-price-total{
width: 5%;
}
.sku{
width: 20%;
}
function updatePrice(){
let items = document.querySelectorAll('.item');
let totalNum = 0;
let totalPrice = 0;
items.forEach(function(item){
if(item.querySelector('.item-checkbox').checked){
let num = item.querySelector('.num').value;
totalNum = totalNum + parseInt(num);
let price = item.querySelector('.price-2').getAttribute('data-price');
totalPrice = totalPrice + parseFloat(price) * num;
}
});
document.querySelector('.check-num').innerText= totalNum;
document.querySelector('.total-price').innerText = totalPrice;
}

window.onload=function(){
let itemCheckboxes = document.querySelectorAll('.item-checkbox');
itemCheckboxes.forEach(function(itemCheckbox){
itemCheckbox.onchange = function(){
updatePrice();
}
});

let minuses = document.querySelectorAll('.num-minus');
minuses.forEach(function(minus){
minus.onclick = function(event){
let num=minus.parentElement.querySelector('.num').value;
if(parseInt(num)>1)
{
minus.parentElement.querySelector('.num').value=parseInt(num)-1;
updatePrice();
}
};
});

let pluses = document.querySelectorAll('.num-plus');
pluses.forEach(function(plus){
plus.onclick = function(event){
let num=plus.parentElement.querySelector('.num').value;
plus.parentElement.querySelector('.num').value=parseInt(num)+1;
updatePrice();
}
});
}

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

閱讀全文

與php加入購物車代碼相關的資料

熱點內容
伺服器為什麼要重裝系統 瀏覽:437
華為嘗鮮怎麼還是原來的安卓系統 瀏覽:594
女主是警察的小說 瀏覽:792
魔獸宏命令是什麼 瀏覽:250
《法國空姐》啄木鳥種子 瀏覽:95
超清播放網站 瀏覽:39
索尼傳送app為什麼華為用不了 瀏覽:225
linux命令at 瀏覽:221
阿里程序員厲害 瀏覽:195
iappqq飛車美化源碼 瀏覽:389
51單片機跑馬燈c程序 瀏覽:930
adm壓縮及解壓代碼 瀏覽:853
xp如何製作列印機伺服器 瀏覽:24
張天佑小說蛇 瀏覽:23
安卓手機如何解壓001文件 瀏覽:150
nx編程考證有什麼要求 瀏覽:524
百度雲資源線免費播放網站 瀏覽:829
啄木鳥最好看的一部 瀏覽:660
iphone如何設置文件夾空白名字 瀏覽:10
失去的眼角膜電影完整版 瀏覽:116