php+pdo實(shí)現(xiàn)的購(gòu)物車類完整示例
本文實(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ì)有所幫助。
相關(guān)文章
深入PHP購(gòu)物車模塊功能分析(函數(shù)講解,附源碼)
本篇文章是對(duì)PHP購(gòu)物車模塊功能進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP中構(gòu)造函數(shù)和析構(gòu)函數(shù)解析
這篇文章主要介紹了PHP中構(gòu)造函數(shù)和析構(gòu)函數(shù)解析,本文用代碼實(shí)例講解了PHP中構(gòu)造函數(shù)和析構(gòu)函數(shù),需要的朋友可以參考下2014-10-10
什么是OneThink oneThink后臺(tái)添加插件步驟
這篇文章主要為大家詳細(xì)介紹了oneThink后臺(tái)添加插件的具體實(shí)現(xiàn)步驟,內(nèi)容簡(jiǎn)單,步驟完整,感興趣的小伙伴們可以參考一下2016-04-04
php運(yùn)行出現(xiàn)Call to undefined function curl_init()的解決方法
curl_init -- 初始化一個(gè)CURL會(huì)話,如果提示Call to undefined function curl_init那么需要如下操作即可。2010-11-11
php壓縮HTML函數(shù)輕松實(shí)現(xiàn)壓縮html/js/Css及注意事項(xiàng)
如何提高網(wǎng)頁(yè)加載速度需要對(duì)網(wǎng)頁(yè)怎樣的優(yōu)化等等,都是站長(zhǎng)們所關(guān)心的問(wèn)題,其實(shí)壓縮網(wǎng)頁(yè)的方法很多,本文將講解一下php壓縮HTML函數(shù)輕松實(shí)現(xiàn)壓縮html/js/Css,感興趣的朋友可以了解下,希望本文對(duì)你有所幫助2013-01-01

