首頁 > 後端開發 > php教程 > 完整範例php+pdo實作的購物車類

完整範例php+pdo實作的購物車類

coldplay.xixi
發布: 2023-04-09 10:38:02
轉載
2866 人瀏覽過

完整範例php+pdo實作的購物車類

本文實例講述了php pdo實作的購物車類別。分享給大家供大家參考,如下:

<?php
session_start();
class Cart
{
  public $pdo = null;
  public function __construct($config)
  {
    $host = $config[&#39;host&#39;];
    $user = $config[&#39;user&#39;];
    $db = $config[&#39;db&#39;];
    $pwd = $config[&#39;pwd&#39;];
    if (empty($_SESSION[&#39;user_id&#39;])) {
      return show(0, &#39;请先登录&#39;);
    }
    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[&#39;price&#39;];
    $createtime = time();
    $sql = "select * from shop_cart where productid=? and userid=?";
    $stmt = $this->pdo->prepare($sql);
    $stmt->execute(array($productid, $_SESSION[&#39;user_id&#39;]));
    $data = $stmt->fetch(PDO::FETCH_ASSOC);
    if ($data) {
      $sql = "update shop_cart set num=num+? where userid=? and productid=?";
      $params = array($num, $_SESSION[&#39;user_id&#39;], $productid);
    } else {
      $sql = "insert into shop_cart(productid,num,userid,price,createtime) values(?,?,?,?,?)";
      $params = array($productid, $num, $_SESSION[&#39;user_id&#39;], $price, $createtime);
    }
    $stmt = $this->pdo->prepare($sql);
    $stmt->execute($params);
    $rows = $stmt->rowCount();
    return $rows ?
      show(1, &#39;ok&#39;, $rows) :
      show(0, &#39;fail&#39;);
  }
  //修改购买数量
  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[&#39;user_id&#39;], $productid));
    $rows = $stmt->rowCount();
    return $rows ?
      show(1, &#39;ok&#39;, $rows) :
      show(0, &#39;fail&#39;);
  }
  //清空购物车
  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, &#39;ok&#39;, $rows) :
      show(0, &#39;fail&#39;);
  }
  //从购物车中删除商品
  public function remove_cart($productid)
  {
    $sql = "delete from shop_cart where productid=? and userid=?";
    $stmt = $this->pdo->prepare($sql);
    $stmt->execute(array($productid, $_SESSION[&#39;user_id&#39;]));
    $rows = $stmt->rowCount();
    return $rows ?
      show(1, &#39;ok&#39;, $rows) :
      show(0, &#39;fail&#39;);
  }
}
//处理数据
function show($status, $message, $data = array())
{
  $result = array(
    &#39;status&#39; => $status,
    &#39;message&#39; => $message,
    &#39;data&#39; => $data
  );
  exit(json_encode($result));
}
//简单使用
$user = [
  &#39;host&#39; => &#39;&#39;,
  &#39;user&#39; => &#39;root&#39;,
  &#39;pwd&#39; => &#39;root&#39;,
  &#39;db&#39; => &#39;shop&#39;,
];
$productid = intval($_POST[&#39;productid&#39;]);
$num = intval($_POST[&#39;num&#39;]);
$cart = new Cart($user);
//添加到购物车
$cart->add_cart($productid, $num);
//删除指定的商品
$cart->remove_cart($productid);
//清空
$cart->clear_cart();
?>
登入後複製

相關學習推薦:PHP程式設計從入門到精通

以上是完整範例php+pdo實作的購物車類的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:jb51.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板