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

前端之家收集整理的这篇文章主要介绍了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,$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,$productid));
    $rows = $stmt->rowCount();
    return $rows ?
      show(1,'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,'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,'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相关内容感兴趣的读者可查看本站专题:《PHP+MysqL购物车开发专题》、《PHP面向对象程序设计入门教程》、《PHP数学运算技巧总结》、《PHP数组(Array)操作技巧大全》、《PHP字符串(string)用法总结》、《PHP数据结构与算法教程》、《PHP程序设计算法总结》、《PHP正则表达式用法总结》、及《PHP常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

原文链接:/php/534413.html

猜你在找的PHP相关文章