濮阳杆衣贸易有限公司

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

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

熱門標(biā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)物車類。分享給大家供大家參考,具體如下:

?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)物車
  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)買數(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)物車
  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)物車中刪除商品
  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)物車
$cart->add_cart($productid, $num);
//刪除指定的商品
$cart->remove_cart($productid);
//清空
$cart->clear_cart();
?>

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP+MySQL購(gòu)物車開發(fā)專題》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《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常見數(shù)據(jù)庫(kù)操作技巧匯總》

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

您可能感興趣的文章:
  • PHP PDO預(yù)處理語(yǔ)句及事務(wù)的使用
  • PHP如何初始化PDO及原始SQL語(yǔ)句操作
  • PHP中PDO關(guā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)物車類完整示例》,本文關(guān)鍵詞  php+pdo,實(shí)現(xiàn),的,購(gòu)物車,類,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《php+pdo實(shí)現(xiàn)的購(gòu)物車類完整示例》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于php+pdo實(shí)現(xiàn)的購(gòu)物車類完整示例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    德令哈市| 大厂| 鄱阳县| 钦州市| 平原县| 军事| 竹溪县| 瑞安市| 资源县| 安徽省| 博罗县| 平乡县| 武宁县| 富蕴县| 汤原县| 黄梅县| 临沧市| 同仁县| 石林| 藁城市| 定州市| 景德镇市| 丹阳市| 安庆市| 鹤壁市| 桂林市| 平和县| 乌兰浩特市| 湖口县| 繁昌县| 中西区| 平阴县| 镇原县| 厦门市| 张家川| 宣恩县| 布尔津县| 丰县| 睢宁县| 安乡县| 新津县|