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();
?>
更多關于PHP相關內(nèi)容感興趣的讀者可查看本站專題:《PHP+MySQL購物車開發(fā)專題》、《php面向對象程序設計入門教程》、《PHP數(shù)學運算技巧總結》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結》、《PHP數(shù)據(jù)結構與算法教程》、《php程序設計算法總結》、《php正則表達式用法總結》、及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
PHP編碼規(guī)范-php coding standard
標準化問題在某些方面上讓每個人頭痛,讓人人都覺得大家處于同樣的境地。這有助于讓這些建議在許多的項目中不斷演進,許多公司花費了許多星期逐子字逐句的進行爭論。2007-03-03
PHP如何使用array_unshift()在數(shù)組開頭插入元素
這篇文章主要介紹了PHP如何使用array_unshift()在數(shù)組開頭插入元素,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-09-09
PHPExcel導出Excel報錯:PHPExcel_IOFactory::load()的解決方案
這篇文章主要介紹了PHPExcel導出Excel報錯:PHPExcel_IOFactory::load()的解決方案,文中有詳細的問題分析和解決方法供大家參考,具有一定的參考價值,需要的朋友可以參考下2023-11-11

