中文字幕第五页-中文字幕第页-中文字幕韩国-中文字幕最新-国产尤物二区三区在线观看-国产尤物福利视频一区二区

示例PHP購物車類Cart.class.php定義與用法

本文實(shí)例講述了PHP購物車類Cart.class.php定義與用法。分享給大家供大家參考,具體如下:

成都創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、欽南網(wǎng)絡(luò)推廣、微信小程序、欽南網(wǎng)絡(luò)營銷、欽南企業(yè)策劃、欽南品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們大的嘉獎;成都創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供欽南建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:m.2m8n56k.cn

之前的開發(fā)人員使用了JS的技術(shù)開發(fā)了一套前臺購物車(刪除添加什么的都使用JS),但是瀏覽器兼容不好, 今天終于出問題了, 有個(gè)老外購物了產(chǎn)品, 由于使用了不知名的瀏覽器, chrome, opera…都有可能, 因此, 我多了一份工作, 重寫購物車.

不打算再使用JS, 直接考慮php.

找到了一個(gè)購物車的類, 使用起來很方便.

Cart.class.php源碼:

<?php
/**
 * Cart
 *
 * 購物車類
 *
 * @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
  *
  * 類的構(gòu)造函數(shù),使購物車保持穩(wěn)定的初始化狀態(tài)
  *
  * @static
  * @access public
  * @return void 無返回值
  * @param void 無參數(shù)
  */
 function Cart(){
 $this->totalCount = 0;
 $this->totalPrice = 0;
 $this->cart = array();
 }
 // }}}
 // {{{ add($item)
 /**
 * 增加商品到當(dāng)前購物車
 *
 * @access public
 * @param array $item 商品信息(一維數(shù)組:array(商品ID,商品名稱,商品單價(jià),商品數(shù)量))
 * @return array 返回當(dāng)前購物車內(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)移動了數(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)前購物車中取出部分或全部商品
 * 當(dāng) $key=="" 的時(shí)候,清空當(dāng)前購物車
 * 當(dāng) $key!=""&&$count=="" 的時(shí)候,從當(dāng)前購物車中揀出商品ID號為 $key 的全部商品
 * 當(dāng) $key!=""&&$count!="" 的時(shí)候,從當(dāng)前購物車中揀出 $count個(gè) 商品ID號為 $key 的商品
 *
 * @access public
 * @param string $key 商品ID
 * @return mixed 返回真假或當(dāng)前購物車內(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==""){ //移去這一類商品
 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)
 /**
 * 修改購物車內(nèi)商品ID為 $key 的商品的數(shù)量為 $value
 *
 * @access public
 * @param string $key 商品ID
 * @param int $value 商品數(shù)量
 * @return array 返回當(dāng)前購物車內(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)前購物車內(nèi)商品的數(shù)組
 *
 * @access public
 * @return array 返回當(dāng)前購物車內(nèi)商品的數(shù)組;
 */
 function getCart(){
 return $this->cart;
 }
 // }}}
 // {{{ _isExists($key)
 /**
 * 判斷當(dāng)前購物車中是否存在商品ID號為$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)前購物車是否為空,即沒有任何商品
 *
 * @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)前購物車所有商品的總金額
 *
 * @access public
 * @return float 返回金額;
 */
 function totalPrices(){
 if($this->_stat())
 return $this->totalPrices;
 return 0;
 }
 // }}}
 // {{{ isEmpty()
 /**
 * 取得當(dāng)前購物車所有商品的總數(shù)量和
 *
 * @access public
 * @return int ;
 */
 function totalCount(){
 if($this->_stat())
 return $this->totalCount;
 return 0;
 }
}//End Class Cart
?>

使用該類的方法:

<?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="\\&quot;index.php\\&quot;" 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="\\&quot;location='?action=remove&key=&quot;.$item['ID'].&quot;'\\&quot;" type="button" value="刪除">"; } echo "<input type="hidden" value="modi" name="action">"; echo "</td></tr><tr><td colspan="7"><input type="submit" value="提交查詢內(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="提交查詢內(nèi)容">
</form></count>

文章題目:示例PHP購物車類Cart.class.php定義與用法
文章網(wǎng)址:http://m.2m8n56k.cn/article30/chesso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站微信公眾號微信小程序移動網(wǎng)站建設(shè)Google網(wǎng)頁設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名
主站蜘蛛池模板: 欧美一级aⅴ毛片 | 2021国产精品自在拍在线播放 | 福利视频在线午夜老司机 | 亚洲性在线观看 | 一级做性色a爰片久久毛片免费 | 亚洲成在人 | 免费人成在线观看播放国产 | 久久久不卡国产精品一区二区 | 麻豆传媒一区 | 国产成人精品福利网站人 | 久久国产精品免费 | 亚洲欧美久久精品1区2区 | 亚洲韩国欧美 | 亚洲一区二区三区不卡在线播放 | 亚洲国产成人久久精品影视 | 狠狠综合久久久久综合小说网 | 久久精品99精品免费观看 | 色视频网站大全免费 | 国产α片 | 久久国产片 | 日韩一级精品久久久久 | 欧美色久 | 欧美亚洲国产精品久久 | 国产三级a三级三级 | 亚洲日本久久一区二区va | 美女张腿男人桶免费视频 | 日本特级黄毛片毛片视频 | 黄色影视频 | 美女视频在线观看黄 | 美国免费高清一级毛片 | 姐姐真漂亮在线视频中文版 | 99在线小视频 | 国产精品aⅴ | 国产自线一二三四2021 | 波多野结衣中文无毒不卡 | 亚洲国产精品久久久久久 | 美女免费毛片 | 国产成人精品一区二区三在线观看 | 农村寡妇一级毛片免费看视频 | 国产国产人免费人成成免视频 | 看一级毛片一区二区三区免费 |