php+pdo实现的购物车类完整示例
吾爱主题
阅读:108
2021-09-27 11:14:00
评论:0
本文实例讲述了php+pdo实现的购物车类。分享给大家供大家参考,具体如下:
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | <?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程序设计有所帮助。
原文链接:https://blog.csdn.net/luyaran/article/details/74748240
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。