<?php /** * @version $Id$ * @create 2013-1-27 21:47:50 By xjiujiu * @description HongJuZi Framework * @copyRight Copyright (c) 2011-2012 http://www.xjiujiu.com.All right reserved */@header('Content-Type: text/html, charset=utf-8;'); //開啟session @session_start(); include_once('./hongjuzi/hhcart.php'); //php 按行執行這雷根據傳來的action參數去調用其中的方法。 //其實柑橘在java中也是一樣的. //在servlet中不也是通過指定方法才去調用doget和dopost方法麼。 //只不過這個是在method中指定的。 //如果要調用其他方法,也要自己去執行個體化的。 //而在struts2中,應該是用!去分割的。 $ac = $_GET['ac']; $cart = new HHCart();switch($ac) { case 'list':try{$list = $cart->getList();foreach($list as $key => $value){$num = $list[$key]['total'];$obj = $list[$key]['info']; //stripslashes(String)去除斜杠$price = stripslashes($obj['price']);$count += intval($num * intval($price));}//echo json_encode(array('list' => $cart->getList())); //建立數組,並複製給$orderList引用.期中list應該是一個對象數組。$orderList = array('list' => $cart->getList(),'count'=>$count);//array_push($orderList); //轉為json格式echo json_encode($orderList);}catch(Exception $ex){echo "失敗";}break; case 'add': { try { //接受資料,並存為個一個數組。 $productInfo = array( 'id' => $_POST['productId'], 'name' => $_POST['productName'], 'price' => $_POST['price'], 'type' => $_POST['productType'] ); //列印調試var_dump($productInfo); $cart->add($productInfo); echo json_encode(array('succeed' => true)); } catch(Exception $ex) { echo json_encode(array( 'succeed' => 'false', 'info' => '貨物添加失敗!' )); } break; } case 'sub': break; case 'destroy': break; default: break; }?>
HHcart類
<?php /** * @version $Id$ * @create 2013-1-27 21:47:50 By xjiujiu * @description HongJuZi Framework * @copyRight Copyright (c) 2011-2012 http://www.xjiujiu.com.All right reserved *//** * 購物車類 * * @desc * * @author xjiujiu <xjiujiu@foxmail.com> * @package None * @since 1.0.0 */class HHCart{ /** * @var Map $_cart 購物車容器 */ protected $_cart; /** * @var String $_cartId 當前的購物車ID */ protected $_cartId; /** * @var String $_priceParam 單價名稱 */ protected $_priceParam; /** * @var int $_productIdParamName 當前的產品ID變量名 */ protected $_productIdParamName; /** * @var Mix $_productId 當前的產品ID */ protected $_productId; /** * 構造函數 * * @desc * * @author xjiujiu <xjiujiu@foxmail.com> * @access public * @param String $price 當前的價格變量名稱,默認:price * @param String $productIdParamName 產品編號變量 */ public function __construct($price = 'price', $productIdParamName = 'id') { $this->_getCurCart(); $this->_cartId = session_id(); $this->_productInfo = null; $this->_priceParam = $price; $this->_productIdParamName = $productIdParamName; } /** * 得到當前的購物車容器 * * @desc * * @author xjiujiu <xjiujiu@foxmail.com> * @access protected */ protected function _getCurCart() { if(!isset($_SESSION['HHCART']) || !is_array($_SESSION['HHCART'])) { $_SESSION['HHCART'] = array(); } $this->_cart = &$_SESSION['HHCART']; } /** * 添加貨物 * * @desc * * @author xjiujiu <xjiujiu@foxmail.com> * @access public */ public function add($productInfo) {$productId = $productInfo[$this->_productIdParamName]; if(!isset($this->_cart[$this->_cartId])) { $this->_cart[$this->_cartId] = array(); } if(isset($this->_cart[$this->_cartId][$productId])) { $this->_cart[$this->_cartId][$productId]['total'] ++; } else { $this->_cart[$this->_cartId][$productId]['total'] = 1; $this->_cart[$this->_cartId][$productId]['info'] = $productInfo; }} /** * 減少貨物 * * @desc * * @author xjiujiu <xjiujiu@foxmail.com> * @access public * @return void * @throws none */ public function sub($productId) { if(isset($this->_cart[$this->_cartId])) { if(isset($this->_cart[$this->_cartId][$productId])) { if(1 != $this->_cart[$this->_cartId][$productId]['total']) { $this->_cart[$this->_cartId][$productId]['total'] -= 1; } else { unset($this->_cart[$this->_cartId][$productId]['total']); } } } } /** * 清除貨物 * * @desc * * @author xjiujiu <xjiujiu@foxmail.com> * @access public * @return void * @throws none */ public function delete($productId) { if(isset($this->_cart[$this->_cartId])) { if(isset($this->_cart[$this->_cartId][$productId])) { unset($this->_cart[$this->_cartId][$productId]); } } } /** * 更新購物車 * * @desc * * @author xjiujiu <xjiujiu@foxmail.com> * @access public * @return List 得到當前購物車裡的內容 */ public function getList() { return $this->_cart[$this->_cartId]; } /** * 清空 * * @desc * * @author xjiujiu <xjiujiu@foxmail.com> * @access public * @return void * @throws none */ public function destroy() { unset($this->_cart[$this->_cartId]); } /** * 結賬 * * @desc * * @author xjiujiu <xjiujiu@foxmail.com> * @access public */ public function count() { $total = 0; foreach($this->_cart[$this->_cartId] as $product) { $total += intval($product['total']) * intval($product['info'][$this->_priceParam]); } return $total; }}?>