php+pdo实现的购物车类完整示例
时间:2022-07-27
本文章向大家介绍php+pdo实现的购物车类完整示例,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
本文实例讲述了php+pdo实现的购物车类。分享给大家供大家参考,具体如下:
<?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');
}
//修改购买数量
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');
}
}
//处理数据
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相关内容感兴趣的读者可查看本站专题:《PHP+MySQL购物车开发专题》、《php面向对象程序设计入门教程》、《PHP数学运算技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《php正则表达式用法总结》、及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
- Windows下安装MariaDB
- Quartz.net官方开发指南 第一课:使用Quartz.net
- Quartz.net官方开发指南 第二课:Jobs And Triggers
- 为你的WordPress 主题添加结构化数据/丰富文本摘要,高亮搜索结果(上)
- 复合事件处理(Complex Event Processing)介绍
- Quartz.net官方开发指南 第三课:更多关于Jobs和JobDetails
- 为你的WordPress 主题添加结构化数据/丰富文本摘要,高亮搜索结果(下)
- Quartz.net官方开发指南 第四课:关于Triggers更多内容
- 数据分析:寻找Python最优计算性能
- 事件流处理框架NEsper for .NET
- Quartz.net官方开发指南 第五课: SimpleTrigger
- SQL Server Performance Dashboard Reports
- 添加WordPress评论输入邮箱实时显示Gravatar头像功能
- Quartz.net官方开发指南 第六课 : CronTrigger
- php概述
- php教程
- php环境搭建
- PHP书写格式
- php变量
- php常量
- PHP注释
- php数组
- php字符串 string
- PHP整型 integer
- PHP浮点型 float
- php布尔型
- php数据类型之数组
- php数据类型之对象
- php数据类型之null
- php数据类型之间的转换
- php运算符
- php表达式
- PHP循环控制
- PHP流程控制
- php函数
- php全局变量
- PHP魔术变量
- php命名空间
- php 日期
- PHP包含文件
- php文件
- PHP 文件上传
- php Cookies
- php Sessions
- php email
- php安全email
- php错误处理
- PHP异常处理
- php过滤器
- PHP 高级过滤器
- php json
- php 表单
- PHP MySQL 简介
- PHP 连接 MySQL
- php创建数据库
- php 创建表
- php mysq 插入数据
- PHP MySQL 插入多条数据
- PHP MySQL 预处理语句
- php mysql 读取数据
- php mysql where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- php ODBC