主頁 > 知識庫 > php+pdo實現(xiàn)的購物車類完整示例

php+pdo實現(xiàn)的購物車類完整示例

熱門標(biāo)簽:河南電銷卡外呼系統(tǒng)哪家強(qiáng) 昭通辦理400電話 青島語音外呼系統(tǒng)招商 騰訊外呼管理系統(tǒng) 山西探意電話機(jī)器人 岳陽外呼型呼叫中心系統(tǒng)在哪里 百應(yīng)電話機(jī)器人服務(wù) 揚(yáng)州地圖標(biāo)注app 山西回?fù)芡夂粝到y(tǒng)

本文實例講述了php+pdo實現(xiàn)的購物車類。分享給大家供大家參考,具體如下:

?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();
?>

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP+MySQL購物車開發(fā)專題》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》、《php正則表達(dá)式用法總結(jié)》、及《php常見數(shù)據(jù)庫操作技巧匯總》

希望本文所述對大家PHP程序設(shè)計有所幫助。

您可能感興趣的文章:
  • PHP PDO預(yù)處理語句及事務(wù)的使用
  • PHP如何初始化PDO及原始SQL語句操作
  • PHP中PDO關(guān)閉連接的方法問題
  • PHP使用PDO 連接與連接管理操作實例分析
  • PHP使用PDO實現(xiàn)mysql防注入功能詳解
  • PHP PDO和消息隊列的個人理解與應(yīng)用實例分析
  • php pdo連接數(shù)據(jù)庫操作示例
  • PHP連接MySQL數(shù)據(jù)庫的三種方式實例分析【mysql、mysqli、pdo】
  • PHP使用PDO創(chuàng)建MySQL數(shù)據(jù)庫、表及插入多條數(shù)據(jù)操作示例
  • php如何用PDO操作大數(shù)據(jù)對象

標(biāo)簽:銅川 湛江 鎮(zhèn)江 黃南 婁底 寶雞 南陽 宜賓

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php+pdo實現(xiàn)的購物車類完整示例》,本文關(guān)鍵詞  php+pdo,實現(xiàn),的,購物車,類,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《php+pdo實現(xiàn)的購物車類完整示例》相關(guān)的同類信息!
  • 本頁收集關(guān)于php+pdo實現(xiàn)的購物車類完整示例的相關(guān)信息資訊供網(wǎng)民參考!
  • 企业400电话

    智能AI客服机器人
    15000

    在线订购

    合计11份范本:公司章程+合伙协议+出资协议+合作协议+股权转让协议+增资扩股协议+股权激励+股东会决议+董事会决议

    推薦文章