gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章php+pdo实现的购物车类完整示例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文实例讲述了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 。

最后此篇关于php+pdo实现的购物车类完整示例的文章就讲到这里了,如果你想了解更多关于php+pdo实现的购物车类完整示例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com