溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在php中使用pdo實(shí)現(xiàn)一個(gè)購物車類

發(fā)布時(shí)間:2021-05-07 15:47:59 來源:億速云 閱讀:141 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章給大家分享的是有關(guān)怎么在php中使用pdo實(shí)現(xiàn)一個(gè)購物車類,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

PHP開發(fā)環(huán)境搭建工具有哪些

一、phpStudy,是一個(gè)新手入門最常用的開發(fā)環(huán)境。二、WampServer,WampServer也同樣的也是和phpStudy一樣操作簡單對小白比較友好。三、XAMPP,XAMPP(Apache+MySQL+PHP+PERL)是一個(gè)功能強(qiáng)大的建站集成軟件包;四、MAMP,MAMP分為兩種MAMP和MAMP Pro for Mac。五、寶塔面板,寶塔面板是一款服務(wù)器管理軟件,支持windows和linux系統(tǒng)。六、UPUPW,UPUPW是目前Windows平臺下最具特色的Web服務(wù)器PHP套件。

具體如下:

<?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');
  }
  //修改購買數(shù)量
  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');
  }
}
//處理數(shù)據(jù)
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實(shí)現(xiàn)一個(gè)購物車類,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI