php+pdo實現(xiàn)的購物車類完整示例
本文實例講述了php+pdo實現(xiàn)的購物車類。分享給大家供大家參考,具體如下:
<?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, '請先登錄');
}
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();
?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP+MySQL購物車開發(fā)專題》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)學(xué)運算技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》、《php正則表達式用法總結(jié)》、及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
PHP中構(gòu)造函數(shù)和析構(gòu)函數(shù)解析
這篇文章主要介紹了PHP中構(gòu)造函數(shù)和析構(gòu)函數(shù)解析,本文用代碼實例講解了PHP中構(gòu)造函數(shù)和析構(gòu)函數(shù),需要的朋友可以參考下2014-10-10
php運行出現(xiàn)Call to undefined function curl_init()的解決方法
curl_init -- 初始化一個CURL會話,如果提示Call to undefined function curl_init那么需要如下操作即可。2010-11-11
php壓縮HTML函數(shù)輕松實現(xiàn)壓縮html/js/Css及注意事項
如何提高網(wǎng)頁加載速度需要對網(wǎng)頁怎樣的優(yōu)化等等,都是站長們所關(guān)心的問題,其實壓縮網(wǎng)頁的方法很多,本文將講解一下php壓縮HTML函數(shù)輕松實現(xiàn)壓縮html/js/Css,感興趣的朋友可以了解下,希望本文對你有所幫助2013-01-01

