php session 购物车(实例)

 

<?php   

/**  

 * session购物车  

 *  

 * @copyright Copyright (c) 

 */  

  

class ModelCart extends Model {   

  

    /**  

     * 构造函数  

     * @param Object $db 数据查询类  

     * @access public  

     * @return void  

     */  

    function __construct($query=null) {   

        parent::__construct($query);   

        session_start();   

    }   

       

    function add($productInfo) {   

        $product = new Mo的lProduct();   

        $productItem = $product->getInfo($productInfo['id']);   

        $info = array(   

                'id' => $productInfo['id'],   

                'product_name' => $productInfo['product_name'],   

                'attribute' => $productInfo['attribute'],   

                'price' => $productInfo['price'],   

                'sn' => $productItem['sn'],   

                'photoUrl' => CFG_URL . 'data/product/'.$productItem['photo'],   

                'number' => 1,   

                );   

        $_SESSION['cartInfo'][$productInfo['id'].'_'.$productInfo['attribute']] = $info;   

    }   

       

    function changeNumber($id,$number) {   

        $_SESSION['cartInfo'][$id]['number'] = $number;   

    }   

    static function count() {   

        session_start();   

        return count($_SESSION['cartInfo']);   

    }   

       

    function getAll() {   

        $items = $_SESSION['cartInfo'];   

        $result = array();   

        foreach ($items  as $key => $item) {   

            $attrs = explo的('||',$item['attribute']);   

            foreach ($attrs as $attr) {   

                $i = explo的('=',$attr);   

                $n = explo的(',',$i[1]);   

                $item[$i[0]] = $n[1];   

                $item[$i[0].'_id'] = $n[0];   

            }   

            $item['prices'] = $item['price']*$item['number'];   

            $item['cart_id'] = urlenco的($item['id'].'_'.$item['attribute']);   

            $result[] = $item;   

        }   

        return $result;   

    }   

    function getCount() {   

        $items = $this->getAll();   

        $count = 0;   

        foreach ($items  as $key => $item) {   

            $count += $item['prices'];   

        }   

        return $count;   

       

    }   

    function clear() {   

        $_SESSION['cartInfo'] = array();   

    }   

       

    function delete($id) {   

        unset($_SESSION['cartInfo'][$id]);   

    }   

  

}   

?>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值