Home > php教程 > PHP源码 > body text

php session 购物车类

PHP中文网
Release: 2016-05-25 17:05:26
Original
1107 people have browsed it

php  session 购物车类

[代码][PHP]代码

<?php
class Cart{
    public function Cart() {
        if(!isset($_SESSION[&#39;cart&#39;])){
            $_SESSION[&#39;cart&#39;] = array();
        }
    }
 
    /*
    添加商品
    param int $id 商品主键
          string $name 商品名称
          float $price 商品价格
          int $num 购物数量
    */
    public  function addItem($id,$name,$price,$num,$img) {
        //如果该商品已存在则直接加其数量
        if (isset($_SESSION[&#39;cart&#39;][$id])) {
            $this->incNum($id,$num);
            return;
        }
        $item = array();
        $item[&#39;id&#39;] = $id;
        $item[&#39;name&#39;] = $name;
        $item[&#39;price&#39;] = $price;
        $item[&#39;num&#39;] = $num;
        $item[&#39;img&#39;] = $img;
        $_SESSION[&#39;cart&#39;][$id] = $item;
    }
 
    /*
    修改购物车中的商品数量
    int $id 商品主键
    int $num 某商品修改后的数量,即直接把某商品
    的数量改为$num
    */
    public function modNum($id,$num=1) {
        if (!isset($_SESSION[&#39;cart&#39;][$id])) {
            return false;
        }
        $_SESSION[&#39;cart&#39;][$id][&#39;num&#39;] = $num;
    }
 
    /*
    商品数量+1
    */
    public function incNum($id,$num=1) {
        if (isset($_SESSION[&#39;cart&#39;][$id])) {
            $_SESSION[&#39;cart&#39;][$id][&#39;num&#39;] += $num;
        }
    }
 
    /*
    商品数量-1
    */
    public function decNum($id,$num=1) {
        if (isset($_SESSION[&#39;cart&#39;][$id])) {
            $_SESSION[&#39;cart&#39;][$id][&#39;num&#39;] -= $num;
        }
 
        //如果减少后,数量为0,则把这个商品删掉
        if ($_SESSION[&#39;cart&#39;][$id][&#39;num&#39;] <1) {
            $this->delItem($id);
        }
    }
 
    /*
    删除商品
    */
    public function delItem($id) {
        unset($_SESSION[&#39;cart&#39;][$id]);
    }
     
    /*
    获取单个商品
    */
    public function getItem($id) {
        return $_SESSION[&#39;cart&#39;][$id];
    }
 
    /*
    查询购物车中商品的种类
    */
    public function getCnt() {
        return count($_SESSION[&#39;cart&#39;]);
    }
     
    /*
    查询购物车中商品的个数
    */
    public function getNum(){
        if ($this->getCnt() == 0) {
            //种数为0,个数也为0
            return 0;
        }
 
        $sum = 0;
        $data = $_SESSION[&#39;cart&#39;];
        foreach ($data as $item) {
            $sum += $item[&#39;num&#39;];
        }
        return $sum;
    }
 
    /*
    购物车中商品的总金额
    */
    public function getPrice() {
        //数量为0,价钱为0
        if ($this->getCnt() == 0) {
            return 0;
        }
        $price = 0.00;
        $data = $_SESSION[&#39;cart&#39;];
        foreach ($data as $item) {
            $price += $item[&#39;num&#39;] * $item[&#39;price&#39;];
        }
        return sprintf("%01.2f", $price);
    }
 
    /*
    清空购物车
    */
    public function clear() {
        $_SESSION[&#39;cart&#39;] = array();
    }
}
Copy after login

                   


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template