濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > php+pdo實(shí)現(xiàn)的購(gòu)物車(chē)類(lèi)完整示例

php+pdo實(shí)現(xiàn)的購(gòu)物車(chē)類(lèi)完整示例

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

本文實(shí)例講述了php+pdo實(shí)現(xiàn)的購(gòu)物車(chē)類(lèi)。分享給大家供大家參考,具體如下:

?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, '請(qǐng)先登錄');
    }
    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();
    }
  }
  //添加商品到購(gòu)物車(chē)
  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');
  }
  //修改購(gòu)買(mǎi)數(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');
  }
  //清空購(gòu)物車(chē)
  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');
  }
  //從購(gòu)物車(chē)中刪除商品
  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));
}
//簡(jiǎn)單使用
$user = [
  'host' => '',
  'user' => 'root',
  'pwd' => 'root',
  'db' => 'shop',
];
$productid = intval($_POST['productid']);
$num = intval($_POST['num']);
$cart = new Cart($user);
//添加到購(gòu)物車(chē)
$cart->add_cart($productid, $num);
//刪除指定的商品
$cart->remove_cart($productid);
//清空
$cart->clear_cart();
?>

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

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

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

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

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php+pdo實(shí)現(xiàn)的購(gòu)物車(chē)類(lèi)完整示例》,本文關(guān)鍵詞  php+pdo,實(shí)現(xiàn),的,購(gòu)物車(chē),類(lèi),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《php+pdo實(shí)現(xiàn)的購(gòu)物車(chē)類(lèi)完整示例》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于php+pdo實(shí)現(xiàn)的購(gòu)物車(chē)類(lèi)完整示例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    宁明县| 梅州市| 博乐市| 中阳县| 汾阳市| 台南县| 九江市| 武山县| 离岛区| 延安市| 台中县| 鄄城县| 石柱| 马山县| 揭阳市| 岱山县| 东源县| 水富县| 中宁县| 梨树县| 栾城县| 莎车县| 石景山区| 东至县| 阳新县| 八宿县| 襄城县| 无锡市| 祁连县| 连平县| 浑源县| 贵州省| 肇源县| 军事| 湘潭县| 岳阳县| 四会市| 天门市| 古蔺县| 武鸣县| 安泽县|