zoukankan      html  css  js  c++  java
  • php 面向对象(不知道取什么名字)

    <?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;
        }
    
    }
    
    ?>


  • 相关阅读:
    linux 笔试题
    shell -Z- d等等代表
    shell中for循环总结
    linux启动过程
    linux面试题3
    linux面试题2
    小峰servlet/jsp(4)EL表达式
    小峰servlet/jsp(3)登陆功能实现
    小峰servlet/jsp(2)
    java日期比较例子等...
  • 原文地址:https://www.cnblogs.com/yangzhi/p/3576571.html
Copyright © 2011-2022 走看看