欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php+pdo实现的购物车类完整示例

程序员文章站 2022-07-22 14:04:19
本文实例讲述了php+pdo实现的购物车类。分享给大家供大家参考,具体如下:

本文实例讲述了php+pdo实现的购物车类。分享给大家供大家参考,具体如下:

<?php
session_start();
class cart
{
  public $pdo = null;
  public function __construct($config)
  {
    $host = $config['host'];
    $user = $config['user'];
    $db = $config['db'];
    $pwd = $config['pwd'];
    if (empty($_session['user_id'])) {
      return show(0, '请先登录');
    }
    try {
      $this->pdo = new pdo("mysql:host=$host;dbname=$db", "$user", "$pwd", array(pdo::attr_errmode => pdo::errmode_exception));
      $this->pdo->query("set names utf8");
    } catch (pdoexception $e) {
      echo $e->getmessage();
    }
  }
  //添加商品到购物车
  public function add_cart($productid, $num)
  {
    $sql = "select price from shop_product where id=?";
    $stmt = $this->pdo->prepare($sql);
    $stmt->execute(array($productid));
    $data = $stmt->fetch(pdo::fetch_assoc);
    $price = $data['price'];
    $createtime = time();
    $sql = "select * from shop_cart where productid=? and userid=?";
    $stmt = $this->pdo->prepare($sql);
    $stmt->execute(array($productid, $_session['user_id']));
    $data = $stmt->fetch(pdo::fetch_assoc);
    if ($data) {
      $sql = "update shop_cart set num=num+? where userid=? and productid=?";
      $params = array($num, $_session['user_id'], $productid);
    } else {
      $sql = "insert into shop_cart(productid,num,userid,price,createtime) values(?,?,?,?,?)";
      $params = array($productid, $num, $_session['user_id'], $price, $createtime);
    }
    $stmt = $this->pdo->prepare($sql);
    $stmt->execute($params);
    $rows = $stmt->rowcount();
    return $rows ?
      show(1, 'ok', $rows) :
      show(0, 'fail');
  }
  //修改购买数量
  public function change_num($productid, $num)
  {
    $sql = "update shop_cart set num=? where userid=? and productid=?";
    $stmt = $this->pdo->prepare($sql);
    $stmt->execute(array($num, $_session['user_id'], $productid));
    $rows = $stmt->rowcount();
    return $rows ?
      show(1, 'ok', $rows) :
      show(0, 'fail');
  }
  //清空购物车
  public function clear_cart()
  {
    $sql = "delete from shop_cart where userid=?";
    $stmt = $this->pdo->prepare($sql);
    $this->pdo->execute(array($this->user_id));
    $rows = $stmt->rowcount();
    return $rows ?
      show(1, 'ok', $rows) :
      show(0, 'fail');
  }
  //从购物车中删除商品
  public function remove_cart($productid)
  {
    $sql = "delete from shop_cart where productid=? and userid=?";
    $stmt = $this->pdo->prepare($sql);
    $stmt->execute(array($productid, $_session['user_id']));
    $rows = $stmt->rowcount();
    return $rows ?
      show(1, 'ok', $rows) :
      show(0, 'fail');
  }
}
//处理数据
function show($status, $message, $data = array())
{
  $result = array(
    'status' => $status,
    'message' => $message,
    'data' => $data
  );
  exit(json_encode($result));
}
//简单使用
$user = [
  'host' => '',
  'user' => 'root',
  'pwd' => 'root',
  'db' => 'shop',
];
$productid = intval($_post['productid']);
$num = intval($_post['num']);
$cart = new cart($user);
//添加到购物车
$cart->add_cart($productid, $num);
//删除指定的商品
$cart->remove_cart($productid);
//清空
$cart->clear_cart();
?>