PHP購(gòu)物車(chē)類(lèi)Cart.class.php定義與用法示例
本文實(shí)例講述了PHP購(gòu)物車(chē)類(lèi)Cart.class.php定義與用法。分享給大家供大家參考,具體如下:
之前的開(kāi)發(fā)人員使用了JS的技術(shù)開(kāi)發(fā)了一套前臺(tái)購(gòu)物車(chē)(刪除添加什么的都使用JS),但是瀏覽器兼容不好, 今天終于出問(wèn)題了, 有個(gè)老外購(gòu)物了產(chǎn)品, 由于使用了不知名的瀏覽器, chrome, opera…都有可能, 因此, 我多了一份工作, 重寫(xiě)購(gòu)物車(chē).
不打算再使用JS, 直接考慮php.
找到了一個(gè)購(gòu)物車(chē)的類(lèi), 使用起來(lái)很方便.
Cart.class.php源碼:
<?php
/**
* Cart
*
* 購(gòu)物車(chē)類(lèi)
*
* @author doodoo<pwtitle @yahoo.com.cn="">
* @package Cart
* @category Cart
* @license PHP License
* @access public
* @version $Revision: 1.10 $
*/
Class Cart{
var $cart;
var $totalCount; //商品總數(shù)量
var $totalPrices; //商品總金額
/**
* Cart Constructor
*
* 類(lèi)的構(gòu)造函數(shù),使購(gòu)物車(chē)保持穩(wěn)定的初始化狀態(tài)
*
* @static
* @access public
* @return void 無(wú)返回值
* @param void 無(wú)參數(shù)
*/
function Cart(){
$this->totalCount = 0;
$this->totalPrice = 0;
$this->cart = array();
}
// }}}
// {{{ add($item)
/**
* 增加商品到當(dāng)前購(gòu)物車(chē)
*
* @access public
* @param array $item 商品信息(一維數(shù)組:array(商品ID,商品名稱(chēng),商品單價(jià),商品數(shù)量))
* @return array 返回當(dāng)前購(gòu)物車(chē)內(nèi)商品的數(shù)組
*/
function add($item){
if(!is_array($item)||is_null($item)) return $this->cart;
if(!is_numeric(end($item))||(!is_numeric(prev($item)))) {
echo "價(jià)格和數(shù)量必須是數(shù)字";
return $this->cart;
}
reset($item); //這一句是必須的,因?yàn)樯厦娴呐袛嘁呀?jīng)移動(dòng)了數(shù)組的指標(biāo)
$key = current($item);
if($key=="") return $this->cart;
if($this->_isExists($key)){ //商品是否已經(jīng)存在?
$this->cart[$key]['count'] = end($item);
return $this->cart;
}
$this->cart[$key]['ID'] = $key;
$this->cart[$key]['name'] = next($item);
$this->cart[$key]['price'] = next($item);
$this->cart[$key]['count'] = next($item);
return $this->cart;
}
// }}}
// {{{ add($item)
/**
* 從當(dāng)前購(gòu)物車(chē)中取出部分或全部商品
* 當(dāng) $key=="" 的時(shí)候,清空當(dāng)前購(gòu)物車(chē)
* 當(dāng) $key!=""&&$count=="" 的時(shí)候,從當(dāng)前購(gòu)物車(chē)中揀出商品ID號(hào)為 $key 的全部商品
* 當(dāng) $key!=""&&$count!="" 的時(shí)候,從當(dāng)前購(gòu)物車(chē)中揀出 $count個(gè) 商品ID號(hào)為 $key 的商品
*
* @access public
* @param string $key 商品ID
* @return mixed 返回真假或當(dāng)前購(gòu)物車(chē)內(nèi)商品的數(shù)組
*/
function remove($key="",$count=""){
if($key=="") {
$this->cart = array();
return true;
}
if(!array_key_exists($key,$this->cart)) return false;
if($count==""){ //移去這一類(lèi)商品
unset($this->cart[$key]);
}else{ //移去$count個(gè)商品
$this->cart[$key]['count'] -= $count;
if($this->cart[$key]['count']<=0) unset($this->cart[$key]);
}
return $this->cart;
}
// }}}
// {{{ modi($key,$value)
/**
* 修改購(gòu)物車(chē)內(nèi)商品ID為 $key 的商品的數(shù)量為 $value
*
* @access public
* @param string $key 商品ID
* @param int $value 商品數(shù)量
* @return array 返回當(dāng)前購(gòu)物車(chē)內(nèi)商品的數(shù)組;
*/
function modi($key,$value){
if(!$this->_isExists($key)) return $this->cart(); //不存在此商品,直接返回
if($value<=0){ // value 太小,全部刪除
unset($this->cart[$key]);
return $this->cart;
}
$this->cart[$key]['count'] = $value;
return $this->cart;
}
/**
* 返回當(dāng)前購(gòu)物車(chē)內(nèi)商品的數(shù)組
*
* @access public
* @return array 返回當(dāng)前購(gòu)物車(chē)內(nèi)商品的數(shù)組;
*/
function getCart(){
return $this->cart;
}
// }}}
// {{{ _isExists($key)
/**
* 判斷當(dāng)前購(gòu)物車(chē)中是否存在商品ID號(hào)為$key的商品
*
* @access private
* @param string $key 商品ID
* @return bool true or false;
*/
function _isExists($key)
{
if(isset($this->cart[$key])&&!empty($this->cart[$key])&&array_key_exists($key,$this->cart))
return true;
return false;
}
// }}}
// {{{ isEmpty()
/**
* 判斷當(dāng)前購(gòu)物車(chē)是否為空,即沒(méi)有任何商品
*
* @access public
* @return bool true or false;
*/
function isEmpty(){
return !count($this->cart);
}
// }}}
// {{{ _stat()
/**
* 取得部分統(tǒng)計(jì)信息
*
* @access private
* @return bool true or false;
*/
function _stat(){
if($this->isEmpty()) return false;
foreach($this->cart as $item){
$this->totalCount = @end($item);
$this->totalPrices = @prev($item);
}
return true;
}
// }}}
// {{{ totalPrices()
/**
* 取得當(dāng)前購(gòu)物車(chē)所有商品的總金額
*
* @access public
* @return float 返回金額;
*/
function totalPrices(){
if($this->_stat())
return $this->totalPrices;
return 0;
}
// }}}
// {{{ isEmpty()
/**
* 取得當(dāng)前購(gòu)物車(chē)所有商品的總數(shù)量和
*
* @access public
* @return int ;
*/
function totalCount(){
if($this->_stat())
return $this->totalCount;
return 0;
}
}//End Class Cart
?>
使用該類(lèi)的方法:
<?php
header("Content-type:text/html;charset=utf8");
//調(diào)用實(shí)例
require_once 'Cart.class.php';
session_start();
if(!isset($_SESSION['cart'])) {
$_SESSION['cart'] = new Cart;
}
$cart =& $_SESSION['cart'];
if( ($_SERVER['REQUEST_METHOD']=="POST")&&($_POST['action']=='add') ){
$p = $_POST['p'];
$items = $cart->add($p);
}
if( ($_GET['action']=='remove')&&($_GET['key']!="") ) {
$items = $cart->remove($_GET['key']);
}
if( ($_SERVER['REQUEST_METHOD']=="POST")&&($_POST['action']=='modi') ){
$key = $_POST['key'];
$value = $_POST['value'];
for($i=0;$i<count ($key);$i="" $items="$cart-" ){="">modi($key[$i],$value[$i]);
}
}
$items = $cart->getCart();
//打印
echo "";
setlocale(LC_MONETARY, 'it_IT');
foreach($items as $item){
echo "";
echo "<table border="1"><tbody><tr><form action="\"index.php\"" method="\" post\??=""></form><td>ID:".$item['ID']."<input type="hidden" value=".$item['ID']." name="key[]">"; echo "</td><td>產(chǎn)品:".$item['name']; echo "</td><td>單價(jià):".$item['price']; echo "</td><td><input value=".$item['count']." name="value[]">"; $sum = $item['count']*$item['price']; echo "</td><td>合計(jì):".round($sum,2); echo "</td><td><input onclick="\"location='?action=remove&key=".$item['ID']."'\"" type="button" value="刪除">"; } echo "<input type="hidden" value="modi" name="action">"; echo "</td></tr><tr><td colspan="7"><input type="submit" value="提交查詢(xún)內(nèi)容">"; echo "</td></tr></tbody></table>";
?>
<hr>
<form action="tmp.php" method="post">
ID:<input name="p[]">
品名:<input name="p[]">
單價(jià):<input name="p[]">
數(shù)量:<input name="p[]">
<input type="hidden" value="add" name="action">
<input type="submit" value="提交查詢(xún)內(nèi)容">
</form></count>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《PHP+MySQL購(gòu)物車(chē)開(kāi)發(fā)專(zhuān)題》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《php正則表達(dá)式用法總結(jié)》、及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
php+xml結(jié)合Ajax實(shí)現(xiàn)點(diǎn)贊功能完整實(shí)例
這篇文章主要介紹了php+xml結(jié)合Ajax實(shí)現(xiàn)點(diǎn)贊功能的方法,以一個(gè)完整實(shí)例形式詳細(xì)分析了XML文件、html頁(yè)面以及Ajax結(jié)合php實(shí)現(xiàn)點(diǎn)贊功能的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-01-01
WordPress網(wǎng)站訪(fǎng)問(wèn)慢解決方案細(xì)圖文教程
這篇文章主要介紹了WordPress網(wǎng)站訪(fǎng)問(wèn)慢解決方案細(xì)圖文教程,wordpress訪(fǎng)問(wèn)慢一直是一個(gè)比較頭疼的問(wèn)題,有正好需要的同學(xué)可以嘗試下,感覺(jué)不錯(cuò)的可以分享給大家2021-03-03
PHP通過(guò)header實(shí)現(xiàn)文本文件下載的代碼
所謂實(shí)現(xiàn)文本文件的下載就是說(shuō)當(dāng)我們點(diǎn)擊一個(gè)文本文件的鏈接的時(shí)候不是打開(kāi)這個(gè)文件,而是彈出一個(gè)下載對(duì)話(huà)框讓我們下載2010-08-08
php 獲取遠(yuǎn)程網(wǎng)頁(yè)內(nèi)容的函數(shù)
獲取遠(yuǎn)程網(wǎng)頁(yè)內(nèi)容的php函數(shù)2009-09-09
PHP實(shí)現(xiàn)的折半查詢(xún)算法示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的折半查詢(xún)算法,結(jié)合完整實(shí)例形式分析了php使用遞歸與非遞歸實(shí)現(xiàn)折半查詢(xún)的算法操作步驟與使用方法,需要的朋友可以參考下2017-10-10
php繼承中方法重載(覆蓋)的應(yīng)用場(chǎng)合
這篇文章主要介紹了php繼承中方法重載(覆蓋)的應(yīng)用場(chǎng)合,以實(shí)例形式較為詳細(xì)的分析了重載的用法與對(duì)應(yīng)的適用情況及使用技巧,需要的朋友可以參考下2015-02-02
phpMyAdmin 鏈接表的附加功能尚未激活的問(wèn)題
安裝了phpMyAdmin之后一直有這個(gè)問(wèn)題。雖然知道自己可能用不到這些功能,但是不解決這個(gè)問(wèn)題總覺(jué)著phpMyAdmin和MySQL的安裝沒(méi)有成功。2010-08-08

