php+pdo實(shí)現(xiàn)的購(gòu)物車(chē)類(lèi)完整示例
本文實(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ì)有所幫助。
相關(guān)文章
php的sso單點(diǎn)登錄實(shí)現(xiàn)方法
這篇文章主要介紹了php的sso單點(diǎn)登錄實(shí)現(xiàn)方法,實(shí)例分析了sso單點(diǎn)登錄的原理與具體實(shí)施步驟,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01PHP編碼規(guī)范-php coding standard
標(biāo)準(zhǔn)化問(wèn)題在某些方面上讓每個(gè)人頭痛,讓人人都覺(jué)得大家處于同樣的境地。這有助于讓這些建議在許多的項(xiàng)目中不斷演進(jìn),許多公司花費(fèi)了許多星期逐子字逐句的進(jìn)行爭(zhēng)論。2007-03-03PHP調(diào)用JAVA的WebService簡(jiǎn)單實(shí)例
本篇文章主要是對(duì)PHP調(diào)用JAVA的WebService簡(jiǎn)單實(shí)例進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-03-03PHP如何使用array_unshift()在數(shù)組開(kāi)頭插入元素
這篇文章主要介紹了PHP如何使用array_unshift()在數(shù)組開(kāi)頭插入元素,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09PHPExcel導(dǎo)出Excel報(bào)錯(cuò):PHPExcel_IOFactory::load()的解決方案
這篇文章主要介紹了PHPExcel導(dǎo)出Excel報(bào)錯(cuò):PHPExcel_IOFactory::load()的解決方案,文中有詳細(xì)的問(wèn)題分析和解決方法供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2023-11-11基于php實(shí)現(xiàn)的php代碼加密解密類(lèi)完整實(shí)例
這篇文章主要介紹了基于php實(shí)現(xiàn)的php代碼加密解密類(lèi),結(jié)合完整實(shí)例形式分析了php針對(duì)php代碼進(jìn)行加密與解密的完整流程,以及加密解密類(lèi)的使用方法,需要的朋友可以參考下2016-10-10