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

<?phpsession_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编程从入门到精通

更多相关文章

  1. 直击php反射学习之不用new方法实例化类操作
  2. 详解PHP网页缓存技术优点及代码实例
  3. php in_array函数用法(实例)
  4. PHP下ajax跨域的解决方案之window.name实例分析详解
  5. 基于PHP微信网页获取用户信息的实例分析
  6. php array_chunk函数用法介绍(实例)
  7. 分析PHP下ajax跨域的解决方案之jsonp实例
  8. 详解php中抓取网页内容的实例
  9. php array_column()函数介绍(实例)

随机推荐

  1. Android内存分析和调优
  2. Android系统启动流程(3) —— 解析System
  3. 浅谈Android的TabHost
  4. Android 开发笔记 4:用模拟器测试Android
  5. Android(安卓)Studio 开发基础经验汇总
  6. Android(安卓)10.0 PackageManagerServic
  7. Android ndk 开发环境搭建
  8. android进阶4step2:Android音视频处理——
  9. 浅析Android线程模型
  10. android Activity设置透明主题样式方法