這篇文章主要講解了“php+pdo實(shí)現(xiàn)的購物車類代碼分享”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“php+pdo實(shí)現(xiàn)的購物車類代碼分享”吧!
我們提供的服務(wù)有:成都網(wǎng)站制作、網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、鎮(zhèn)賚ssl等。為近千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的鎮(zhèn)賚網(wǎng)站制作公司本文實(shí)例講述了php+pdo實(shí)現(xiàn)的購物車類。分享給大家供大家參考,具體如下:
<?phpsession_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(); } } //添加商品到購物車 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)的購物車類代碼分享”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)php+pdo實(shí)現(xiàn)的購物車類代碼分享這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
網(wǎng)站名稱:php+pdo實(shí)現(xiàn)的購物車類代碼分享-創(chuàng)新互聯(lián)
本文URL:http://m.newbst.com/article34/disspe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、小程序開發(fā)、企業(yè)建站、外貿(mào)建站、網(wǎng)站設(shè)計(jì)、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容