日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

[share]PHP购物车类的源码

發布時間:2025/7/14 php 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [share]PHP购物车类的源码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

<?php
//調用實例
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 "<table border=1>";
setlocale(LC_MONETARY, 'it_IT');
foreach($items as $item){
echo "<tr><form method=\"post\" action=\"tmp.php\">";
echo "<td>ID:".$item['ID']."<input type=hidden name=key[] value=".$item['ID'].">";
echo "<td>產品:".$item['name'];
echo "<td>單價:".$item['price'];
echo "<td><input type=text name=value[] value=".$item['count'].">";
$sum = $item['count']*$item['price'];
echo "<td>合計:".round($sum,2);
echo "<td><input type=button value='刪除' οnclick=\"location='?action=remove&key=".$item['ID']."'\">";
}
echo "<input type=hidden name=action value=modi>";
echo "<tr><td colspan=7><input type=submit />";
echo "</td></form></tr></table>";


?>
<hr>
<form method="post" action="tmp.php">
ID
:<input type="text" name="p[]" />
品名:
<input type="text" name="p[]" />
單價:
<input type="text" name="p[]" />
數量:
<input type="text" name="p[]" />
<input type=hidden name=action value=add>
<input type="submit" />
</form>

<?
/**
* 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; //商品總數量
var $totalPrices; //商品總金額

/**
* Cart Constructor
*
* 類的構造函數,使購物車保持穩定的初始化狀態
*
* @static
* @access public
* @return void 無返回值
* @param void 無參數
*/
function Cart(){
$this->totalCount = 0;
$this->totalPrice = 0;
$this->cart = array();
}

// }}}
// {{{ add($item)


/**
* 增加商品到當前購物車
*
* @access public
* @param array $item 商品信息(一維數組:array(商品ID,商品名稱,商品單價,商品數量))
* @return array 返回當前購物車內商品的數組
*/
function add($item){
if(!is_array($item)||is_null($item)) return $this->cart;
if(!is_numeric(end($item))||(!is_numeric(prev($item)))) {
echo "價格和數量必須是數字";
return $this->cart;
}
reset($item); //這一句是必須的,因為上面的判斷已經移動了數組的指標
$key = current($item);
if($key=="") return $this->cart;
if($this->_isExists($key)){ //商品是否已經存在?
$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)


/**
* 從當前購物車中取出部分或全部商品
* 當 $key=="" 的時候,清空當前購物車
* 當 $key!=""&&$count=="" 的時候,從當前購物車中揀出商品ID號為 $key 的全部商品
* 當 $key!=""&&$count!="" 的時候,從當前購物車中揀出 $count個 商品ID號為 $key 的商品
*
* @access public
* @param string $key 商品ID
* @return mixed 返回真假或當前購物車內商品的數組
*/
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個商品
$this->cart[$key]['count'] -= $count;
if($this->cart[$key]['count']<=0) unset($this->cart[$key]);
}
return $this->cart;
}

// }}}
// {{{ modi($key,$value)


/**
* 修改購物車內商品ID為 $key 的商品的數量為 $value
*
* @access public
* @param string $key 商品ID
* @param int $value 商品數量
* @return array 返回當前購物車內商品的數組;
*/
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;
}


/**
* 返回當前購物車內商品的數組
*
* @access public
* @return array 返回當前購物車內商品的數組;
*/
function getCart(){
return $this->cart;
}

// }}}
// {{{ _isExists($key)


/**
* 判斷當前購物車中是否存在商品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()


/**
* 判斷當前購物車是否為空,即沒有任何商品
*
* @access public
* @return bool true or false;
*/
function isEmpty(){
return !count($this->cart);
}

// }}}
// {{{ _stat()


/**
* 取得部分統計信息
*
* @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()


/**
* 取得當前購物車所有商品的總金額
*
* @access public
* @return float 返回金額;
*/
function totalPrices(){
if($this->_stat())
return $this->totalPrices;
return 0;
}

// }}}
// {{{ isEmpty()


/**
* 取得當前購物車所有商品的總數量和
*
* @access public
* @return int ;
*/
function totalCount(){
if($this->_stat())
return $this->totalCount;
return 0;
}


}
//End Class Cart
?>

轉載于:https://www.cnblogs.com/c3055/archive/2011/08/24/2151649.html

總結

以上是生活随笔為你收集整理的[share]PHP购物车类的源码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。