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

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

目前创新互联公司已为近1000家的企业提供了网站建设、域名、网站空间网站托管、企业网站设计、沧州网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
<?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();
?>

网站名称:完整示例php+pdo实现的购物车类
标题链接:https://www.cdcxhl.com/article30/cgcdso.html

成都网站建设公司_创新互联,为您提供静态网站品牌网站设计外贸网站建设App设计品牌网站建设移动网站建设

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都定制网站建设