-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.php
217 lines (193 loc) · 5.47 KB
/
cart.php
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
//index.php
$connect = new PDO("mysql:host=localhost;dbname=nnk_fods", "root", "");
$message = '';
setcookie('shopping_cart', array(), time() + (86400 * 30));
if(isset($_POST["add_to_cart"]))
{
if(isset($_COOKIE["shopping_cart"]))
{
$cookie_data = stripslashes($_COOKIE['shopping_cart']);
$cart_data = json_decode($cookie_data, true);
}
else
{
$cart_data = array();
}
$item_id_list = array_column($cart_data, 'item_id');
if(in_array($_POST["hidden_id"], $item_id_list))
{
foreach($cart_data as $keys => $values)
{
if($cart_data[$keys]["item_id"] == $_POST["hidden_id"])
{
$cart_data[$keys]["item_quantity"] = $cart_data[$keys]["item_quantity"] + $_POST["quantity"];
}
}
}
else
{
$item_array = array(
'item_id' => $_POST["hidden_id"],
'item_name' => $_POST["hidden_name"],
'item_price' => $_POST["hidden_price"],
'item_quantity' => $_POST["quantity"]
);
$cart_data[] = $item_array;
}
$item_data = json_encode($cart_data);
setcookie('shopping_cart', $item_data, time() + (86400 * 30));
header("location:index.php?success=1");
}
if(isset($_GET["action"]))
{
if($_GET["action"] == "delete")
{
$cookie_data = stripslashes($_COOKIE['shopping_cart']);
$cart_data = json_decode($cookie_data, true);
foreach($cart_data as $keys => $values)
{
if($cart_data[$keys]['item_id'] == $_GET["id"])
{
unset($cart_data[$keys]);
$item_data = json_encode($cart_data);
setcookie("shopping_cart", $item_data, time() + (86400 * 30));
header("location:index.php?remove=1");
}
}
}
if($_GET["action"] == "clear")
{
setcookie("shopping_cart", "", time() - 3600);
header("location:index.php?clearall=1");
}
}
// set message
if(isset($_GET["success"]))
{
$message = '
<div class="alert alert-success alert-dismissible">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
Item Added into Cart
</div>
';
}
if(isset($_GET["remove"]))
{
$message = '
<div class="alert alert-success alert-dismissible">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
Item removed from Cart
</div>
';
}
if(isset($_GET["clearall"]))
{
$message = '
<div class="alert alert-success alert-dismissible">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
Your Shopping Cart has been clear...
</div>
';
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Webslesson Demo | Simple PHP Mysql Shopping Cart</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<br />
<div class="container">
<section>
<h3 align="center">Simple PHP Mysql Shopping Cart using Cookies</h3><br />
<br /><br />
<?php
$query = "SELECT * FROM tbl_product ORDER BY id ASC";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
{
?>
<div class="col-md-3">
<form method="post">
<div style="border:1px solid #333; background-color:#f1f1f1; border-radius:5px; padding:16px;" align="center">
<img src="images/<?php echo $row["image"]; ?>" class="img-responsive" /><br />
<h4 class="text-info"><?php echo $row["name"]; ?></h4>
<h4 class="text-danger">$ <?php echo $row["price"]; ?></h4>
<input type="text" name="quantity" value="1" class="form-control" />
<input type="hidden" name="hidden_name" value="<?php echo $row["name"]; ?>" />
<input type="hidden" name="hidden_price" value="<?php echo $row["price"]; ?>" />
<input type="hidden" name="hidden_id" value="<?php echo $row["id"]; ?>" />
<input type="submit" name="add_to_cart" style="margin-top:5px;" class="btn btn-success" value="Add to Cart" />
</div>
</form>
</div>
<?php
}
?>
</section>
<section>
<div style="clear:both"></div>
<br />
<h3>Order Details</h3>
<div class="table-responsive">
<?php echo $message; ?>
<div align="right">
<a href="index.php?action=clear"><b>Clear Cart</b></a>
</div>
<table class="table table-bordered">
<tr>
<th width="40%">Item Name</th>
<th width="10%">Quantity</th>
<th width="20%">Price</th>
<th width="15%">Total</th>
<th width="5%">Action</th>
</tr>
<?php
if(isset($_COOKIE["shopping_cart"]))
{
$total = 0;
$cookie_data = stripslashes($_COOKIE['shopping_cart']);
$cart_data = json_decode($cookie_data, true);
foreach($cart_data as $keys => $values)
{
?>
<tr>
<td><?php echo $values["item_name"]; ?></td>
<td><?php echo $values["item_quantity"]; ?></td>
<td>$ <?php echo $values["item_price"]; ?></td>
<td>$ <?php echo number_format($values["item_quantity"] * $values["item_price"], 2);?></td>
<td><a href="index.php?action=delete&id=<?php echo $values["item_id"]; ?>"><span class="text-danger">Remove</span></a></td>
</tr>
<?php
$total = $total + ($values["item_quantity"] * $values["item_price"]);
}
?>
<tr>
<td colspan="3" align="right">Total</td>
<td align="right">$ <?php echo number_format($total, 2); ?></td>
<td></td>
</tr>
<?php
}
else
{
echo '
<tr>
<td colspan="5" align="center">No Item in Cart</td>
</tr>
';
}
?>
</table>
</div>
</section>
</div>
<br />
</body>
</html>