-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBag.php
217 lines (197 loc) · 7.42 KB
/
Bag.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
session_start();
if (isset($_SESSION["user_id"])) {
$mysqli = require __DIR__ . "/database.php"; //connected
$sql = "SELECT * FROM user
WHERE id = {$_SESSION["user_id"]}"; //retrive users with id
$result = $mysqli->query($sql);
$user = $result->fetch_assoc();
}
?>
<?php // guidance from https://www.youtube.com/watch?v=IO5ezsURqyg&t=1221s
session_start();
$database_name = "product_list"; // name of the database in mySQL
$con = mysqli_connect("localhost","root","",$database_name); // connect to product_list
if (isset($_POST["add"])){ // check if "add to bag" is clicked
if (isset($_SESSION["cart"])){
$item_array_id = array_column($_SESSION["cart"],"product_id"); // find IDs of products in the array cart
if (!in_array($_GET["id"],$item_array_id)){ // if not in cart already
$count = count($_SESSION["cart"]); // create a new item for the cart and add it to the end of the cart array.
$item_array = array(
'product_id' => $_GET["id"],
'item_name' => $_POST["hidden_name"],
'product_price' => $_POST["hidden_price"],
'item_quantity' => $_POST["quantity"],
);
$_SESSION["cart"][$count] = $item_array;
echo '<script>window.location="Bag.php"</script>'; // redirect to cart page
}else{
$temp_quantity = 0;
foreach ($_SESSION["cart"] as $keys => $value){
if ($value["product_id"] == $_GET["id"]){
$temp_quantity = $value['item_quantity'];
unset($_SESSION["cart"][$keys]);
}
}
$count = count($_SESSION["cart"]); // create a new item for the cart and add it to the end of the cart array.
$item_array = array(
'product_id' => $_GET["id"],
'item_name' => $_POST["hidden_name"],
'product_price' => $_POST["hidden_price"],
'item_quantity' => $temp_quantity + $_POST["quantity"],
// 'user' => htmlspecialchars($user["name"])
);
$_SESSION["cart"][$count] = $item_array;
echo '<script>window.location="Bag.php"</script>';
}
}else{
$item_array = array( // if cart session doesn't exist yet create cart
'product_id' => $_GET["id"],
'item_name' => $_POST["hidden_name"],
'product_price' => $_POST["hidden_price"],
'item_quantity' => $_POST["quantity"],
);
$_SESSION["cart"][0] = $item_array;
}
}
if (isset($_GET["action"])){
if ($_GET["action"] == "delete"){
foreach ($_SESSION["cart"] as $keys => $value){
if ($value["product_id"] == $_GET["id"]){
unset($_SESSION["cart"][$keys]);
}
}
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Shopping Cart</title>
<style>
*{
font-family: Apercu,Gill Sans,sans-serif;
background-color: hsl(213deg 85% 97%);
}
body {
text-align: left;
background: hsl(213deg 85% 97%);
font-family: Apercu,Gill Sans,sans-serif;
color:hsl(233deg 36% 38%);
}
p{
color:rgb(89, 85, 85);
}
.topnav {
overflow: hidden;
background-color: hsl(213deg 85% 97%);
border: 1px solid #7f7fa6;
position: fixed;
top: 0px;
left: 0px;
width: 100%;
}
.topnav a {
float: left;
color: hsl(233deg 36% 38%);
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #7f7fa6;
color: white;
}
.topnav-right {
float: right;
}
.product{
border: 1px solid black;
margin: -1px 19px 3px -1px;
padding: 10px;
text-align: center;
background-color: #efefef;
}
table, th, tr{
text-align: center;
}
.title2{
text-align: center;
color: white;
background-color: hsl(233deg 36% 38%);
padding: 2%;
}
h2{
text-align: center;
color: hsl(233deg 36% 38%);
padding: 2%;
}
table th{
background-color: #efefef;
}
.btn btn-success{
color: hsl(233deg 36% 38%);
}
</style>
</head>
<body>
<div class="topnav">
<a href="home.php">Home</a>
<a href="makeup.php">Makeup</a>
<div class="topnav-right">
<a class="active" href="Bag.php">Bag</a>
<a href="login.php">Log Out</a>
</div>
</div>
<h2><a href="makeup.php">Back to Products</a></h2>
<h2><?= htmlspecialchars($user["name"]) ?>'s Cart</h2>
<div style="clear: both"></div>
<h3 class="title2">Shopping Cart Details</h3>
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th width="28%">Product Name</th>
<th width="12%">Quantity</th>
<th width="10%">Price Details</th>
<th width="13%">Total Price</th>
<th width="14%">Remove Item</th>
</tr>
<?php
if(!empty($_SESSION["cart"])){
$total = 0;
foreach ($_SESSION["cart"] as $key => $value) {
?>
<tr>
<!-- displays name, quantity, price, and total -->
<td><?php echo $value["item_name"]; ?></td>
<td><?php echo $value["item_quantity"]; ?></td>
<td>$ <?php echo $value["product_price"]; ?></td>
<td>
$ <?php echo number_format($value["item_quantity"] * $value["product_price"], 2); ?></td>
<td><a href="Bag.php?action=delete&id=<?php echo $value["product_id"]; ?>"><span
class="text-danger">Remove Item</span></a></td>
</tr>
<?php
$total = $total + ($value["item_quantity"] * $value["product_price"]); // calculates cart toal
}
?>
<tr>
<td colspan="3" align="right">Total</td>
<th align="right">$ <?php echo number_format($total, 2); ?></th>
<td></td>
</tr>
<?php
}
?>
</div>
</body>
</html>