-
Notifications
You must be signed in to change notification settings - Fork 1
/
cart.php
345 lines (315 loc) · 11 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
#connection to mySQL
# select * from menu
# select * from menuC
# fill arrays with menu info
# only when someone presses the 'submit order' button on the checkout page, then the orderID value will be updated to +1
# checkout to contain requested delivery time (cannot be same day delivery)
session_start();
if (!isset($_SESSION['cart'])){
$_SESSION['cart'] = array();
}
if (!isset($_SESSION['qty'])){
$_SESSION['qty']= array();
}
if (isset($_GET['empty'])) {
unset($_SESSION['cart']);
unset($_SESSION['qty']);
header('location: ' . $_SERVER['PHP_SELF']);
exit();
}
if (isset($_GET['delete'])) {
$i = $_GET['delete'];
unset($_SESSION["cart"][$i]);
unset($_SESSION["qty"][$i]);
#$_SESSION['cart'][] = $_GET['buy'];
header('location: ' . $_SERVER['PHP_SELF']. '?' . SID);
exit();
}
$servername = "localhost";
$username = "f31ee";
$password = "f31ee";
$dbname = "f31ee";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
echo "<script type='text/javascript'>alert('Connecting to database failed!');</script>";
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM `menu`";
if($result= mysqli_query($conn, $sql)){
#$num_rows = mysql_num_rows($result);
$num_rows = mysqli_num_rows($result); #for home use cos PHP 7
$menuID = array_fill(0, $num_rows, 0);
$foodname = array_fill(0, $num_rows, 0);
$price = array_fill(0, $num_rows, 0);
$i = 0;
while($row = mysqli_fetch_assoc($result)) {
#echo $row['timestamp'] . "<br>";
#echo $row['OrderID'] . "<br>";
$menuID[$i] = $row['menuID'];
$foodname[$i] = $row['foodname'];
$price[$i] = $row['price'];
$i++;
}
}
?>
<html lang="en">
<head>
<title>Shopping Cart</title>
<meta charset="utf-8">
<link rel="stylesheet" href="JHYJCSS.css">
<style>
#contenttitle {
margin-top: 30px;
margin-bottom: 50px;
text-align:center;
font-size: 200%;
color: #683e06;
border-bottom: 3px solid #683e06;
}
#desc {
font-size: 130%;
padding-bottom: 30px;
text-align: center;
color: #683e06;
}
#desc table {
margin: auto;
text-align: center;
color: #683e06;
}
#desc th, td, tr {
padding: 5px;
font-size: 115%
}
#desc input[type="submit"] {
margin: auto;
text-align: center;
}
.mid {
margin: auto;
height: 1130px;
}
#desc table thead tr, tfoot tr {
background-color: #d3b792d8;
}
#desc table tbody tr {
background-color: #dfccb3d8;
}
#desc table, tr{
border: 2px solid #683e06;
}
#desc input[type="checkbox"] {
width: 20px;
height: 20px;
}
#desc input[type="number"] {
width: 100px;
height: 30px;
border-radius: 5px;
font-size: 80%;
}
#desc input[type="submit"] {
width: 80px;
height: 30px;
margin-top: 10px;
font-size: 55%;
border-radius: 5px;
color: #683e06;
}
#desc input[type="button"] {
width: 80px;
height: 30px;
margin-top: 10px;
font-size: 55%;
border-radius: 5px;
color: #683e06;
}
#desc table tbody #delete {
color: #683e06;
}
.tablenav {
text-align: right;
}
body {
background-color: #bdc0c9;
background-image: url('background3.jpg');
background-size: auto;
background-repeat: no-repeat;
}
</style>
</head>
<?php
echo "<script type='text/javascript'>
window.cart_list_quantity = new Array;
window.cart_list_price = new Array;
window.looplength = ".count($menuID).";
</script>";
for($a=0; $a < count($menuID); $a++){
echo "<script type='text/javascript'>
window.cart_list_quantity[". $a ."] = ". $_SESSION['qty'][$a] .";
window.cart_list_price[". $a ."] = " . $price[$a] . ";
</script>";
}
?>
<script>
function add(accumulator, a) {
return accumulator + a;
}
window.price_calc = new Array();
window.totalp = 0.0;
var total = 0.0;
//On load, cart quantities loaded
function qty_edit(qtyid, value){
for(var i=0;i<window.looplength;i++){
boxnumber = "box"+i;
qtyboxnumber = "qtybox"+i;
if(document.getElementById(qtyboxnumber) != null){
var qtynum = document.getElementById(qtyboxnumber).value;
}
if(document.getElementById(boxnumber) != null){
if(document.getElementById(boxnumber).checked == true){
window.price_calc[i] = window.cart_list_price[i]*qtynum;
//alert(window.price_calc[i]);
} else {
window.price_calc[i] = 0;
}
}
}
const total = window.price_calc.reduce(add,0);
//alert(total);
//alert("WHY");
//alert("WHY");
document.getElementById("total").innerHTML = '$' + total;
}
function enable() {
var numrows = document.getElementById("cartlist").rows.length;
if(numrows >= 3) {
document.getElementById("message").innerHTML = "<b>Please Select An Item To Checkout.</b>";
document.getElementById("message").style.color = "#e0150e";
document.getElementById("cout").style.border = "2px solid #e0150e";
check();
} else {
return false;
}
}
function check() {
var totalString = document.getElementById("total").innerHTML;
var total = parseFloat(totalString.substring(1));
if(total == 0) {
document.getElementById("message").innerHTML = "<b>Please Select An Item To Checkout.</b>";
document.getElementById("message").style.color = "#e0150e";
document.getElementById("cout").style.border = "2px solid #e0150e";
document.getElementById("cout").disabled = true;
return false;
} else {
document.getElementById("cout").type = "submit";
document.getElementById("cout").removeAttribute("disabled");
document.getElementById("message").innerHTML = "";
document.getElementById("message").style.color = "";
document.getElementById("cout").style.border = "";
return true;
}
}
</script>
<body onload="enable()">
<div class="wrapper">
<div class="top">
<header>
<div id="navigation">
<a href="index.html">
<img src="logo2.png" id="logo" alt="Home" style="width:300px;height:150px;border:0;">
</a>
<div class="tablenav">
<table align="right" id="navbar">
<tr>
<td colspan="2"></td>
<td align="right"><a href="cart.php" id="cart"><img src="cart.png" alt="Cart" style="width: 50px; height: 50px; border:0;"></a></td>
</tr>
<tr align="center">
<td><div class="dropmenu">
<b>MENU</b>
<div class="dropcontent">
<b><a href="classic.php">CLASSIC</a></b>
<b><a href="custom.php">CUSTOM</a></b>
</div>
</div></td>
<td><b><a href="track.html">TRACK ORDER</a></b></td>
<td><b><a href="about.html" id="about">ABOUT US</a></b></td>
</tr>
</table>
</div>
</div>
</header>
</div>
<div class="mid">
<div id="contenttitle">
<b><p>YOUR CART</p></b>
</div>
<div id="desc">
<table border="1" id="cartlist">
<thead>
<tr>
<th>Delete</th>
<th>Selected</th>
<th>Item Description</th>
<th>Quantity</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<form action="checkout.php" method="post">
<?php
$total = 0;
for ($i=0; $i < count($menuID/*$_SESSION['cart']*/); $i++){
if (!empty($_SESSION['qty'][$i])) {
echo "<tr>";
echo "<td><a href='" .$_SERVER['PHP_SELF']. '?delete=' . $i . "' id='delete'>Delete</a></td>";
echo "<td><input type=\"checkbox\" name=\"box" . $i . "\" id=\"box" . $i . "\" value='0' onclick=\"qty_edit('" . $i . "', this.value)\" onchange=\"check()\"></td>";
echo "<td>" .$foodname[$_SESSION['cart'][$i]]. "</td>";
echo "<td><input type=\"number\" name=\"qtybox" . $i . "\" id=\"qtybox" . $i . "\" min='1' value='" . $_SESSION['qty'][$i] . "' onchange=\"qty_edit('" . $i . "', this.value)\"></td>";
echo "<td align='right'". $i .">$";
echo number_format($price[$_SESSION['cart'][$i]], 2). "</td>";
echo "</tr>";
}
#echo "<tr>";
#echo "<td>" .$_SESSION['cart'][$i]['buy']
$box = 'box' . $i;
#$total = $total + $price[$_SESSION['cart'][$i]];
}
?>
</tbody>
<tfoot>
<tr>
<th align='right' colspan="4">Total:</th><br>
<td align='right'><p id="total">$0</p>
</td>
</tr>
</tfoot>
</table>
<p id='message' style="font-size: 80%;"><p>
<p id="checkout"><input id='cout' type='button' value='Checkout!' disabled></p>
</form>
<div>
<p><a href="classic.php" style="font-size: 70%; color: #683e06;">Continue Shopping</a>
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?empty=1" style="font-size: 70%; color: #683e06;">Empty your Cart</a></p>
</div>
</div>
</div>
</div>
<div class="bot">
<footer>
<hr>
<br>
<div id="tm">
<small><i><b>Copyright © 2019 JHYJCakes</b></i></small>
</div>
<div id="links"><b>
<a href="about.html#left">CONTACT US</a>
<a href=""></a>
</b></div>
</footer>
</div>
</body>
</html>