首页 > 作文

php+pdo实现的购物车类完整示例

更新时间:2023-04-08 03:37:26 阅读: 评论:0

本文实例讲述了php+pdo实现的购物车类。分享给大家供大家参考,具体如下:

<?phpssion_start();class cart{  public $pdo = null;  public function __construct($config)  {    $host = $config['host'];    $ur = $config['ur'];    $db = $config['db'];    $pwd = $config['pwd'];    if (empty($_ssion['ur_id'])) {      return show(0, '请先登录');    }    try {      $this->pdo = new pdo("mysql:host=$host;dbname=$db", "$ur", "$pwd", array(pdo::attr_errmode => pdo::errmode_exception));      $this->pdo->query("t names utf8");    } catch (pdoexception $e) {      echo $e->getmessage();    }  }  //添加商品到购物车  public function add_cart($productid, $num)  {    $sql = "lect 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 = "lect * from shop_cart where productid=? and uri新思想d=?";    $stmt = $this->pdo->prepare($sql);    $stmt->execute(array($productid, $_ssion['ur_id']));    $data = $stmt->fetch(pdo::fetch_assoc);    if ($data) {      $sql = "update shop_cart t num=num+? where urid=? and productid=?";      $params = array($num, $_ssion['ur_id'], $productid);    } el {      $sql = "inrt into shop_cart(productid,num,urid,price,createtime) values(?,?,?,?,?)";      $params = array($productid, $num, $_ssion['ur_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 t num=? where urid=? and productid=?";    $stmt = $this->pdo->prepare($sql);    $stmt->execute(array($num, $_ssion['ur_id'], $produ编程入门先学什么ctid));    $rows = $stmt->rowcount();    return $rows ?      show(1, 'ok', $rows) :      show(0, 'fail');  }  //清空购物车  public function c坏账lear_cart()  {    $sql = "delete from shop_cart where urid=?";    $stmt = $this->pdo->prepare($sql);   廉洁在我心中 $this->pdo->execute(array($this->ur_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 urid=?";    $stmt = $this->pdo->prepare($sql);    $stmt->execute(array($productid, $_ssion['ur_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));}//简单使用$ur = [  'host' => '',  'ur' => 'root',  'pwd' => 'root',  'db' => 'shop',];$productid = intval($_post['productid']);$num = intval($_post['num']);$cart = new cart($ur);//添加到购物车$cart->add_cart($productid, $num);//删除指定的商品$cart->remove_cart($productid);//清空$cart->clear_cart();?>

本文发布于:2023-04-08 03:37:24,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/86ee27a8a21748cefa0eb31a78fc68f8.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:php+pdo实现的购物车类完整示例.doc

本文 PDF 下载地址:php+pdo实现的购物车类完整示例.pdf

标签:购物车   商品   清空   在我
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图