-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorder_page.php
More file actions
377 lines (314 loc) · 10.6 KB
/
Copy pathorder_page.php
File metadata and controls
377 lines (314 loc) · 10.6 KB
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
// Initialize the session
session_start();
// Check if the user is logged in, if not then redirect him to login page
if (!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true) {
header("location: login.php");
exit;
}
require_once "config.php";
$current_username = $_SESSION["username"];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Order Page</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
body{ font: sans-serif; text-align: center;}
/* style for popups. never got used
.popup {
visibility: hidden;
position: center;
display: inline-block;
cursor: pointer;
}*/
/*The actual popup (appears on top)
.popup .popuptext {
visibility: hidden;
width: 160px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 8px 0;
position: absolute;
z-index: 1;
bottom: 125%;
left: 50%;
margin-left: -80px;
}*/
/* Popup arrow
.popup .popuptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #555 transparent transparent transparent;
}*/
/* Toggle this class when clicking on the popup container (hide and show the popup)
.popup .show {
visibility: visible;
}
// Add animation (fade in the popup)
@-webkit-keyframes fadeIn {
from {opacity: 0;}
to {opacity: 1;}
}
@keyframes fadeIn {
from {opacity: 0;}
to {opacity:1 ;}
}
*/
/* Style for buttons */
button {
padding: 10px;
margin: 5px;
background-color: #3498db;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #2980b9;
}
table {
border-collapse: collapse;
width: 100%;
}
table, th, td {
border: 1px solid #ddd;
}
th, td {
padding: 10px;
text-align: left;
}
/* Style for modals was never used */
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
/* Modal Content */
.modal-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
/* The Close Button */
.close {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
</style>
<p><nav class="nav justify-content-center">
<a href="welcome.php" class="nav-item nav-link active">Home</a>
<a href="account-info.php" class="nav-item nav-link active">Account info</a>
<a href="surveyList.php" class="nav-item nav-link">Complete surveys</a>
<a href="order_page.php" class="nav-item nav-link">orders/checkout</a>
</nav></p>
<!-- JavaScript for handling submission -->
<script>
/*function closePopup(type) {
var popup=document.getElementByID(type);
popup.classlist.toggle("show");
}*/
function placeOrder() {
// Submit the form to process the order
document.querySelector("form").submit();
}
function cancelOrder() {
document.querySelector("form").reset();
}
/*var modal = document.getElementByID("placedModal");
var btn = document.getElementByID("placeBtn");
var span = document.getElementsByClassName("close")[0];
btn.onclick = function() {
modal.style.display = "block";
}
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}*/
</script>
</head>
<body>
<?php
// Database configuration
$servername = "localhost";
$username = "kguzy";
$password = "s39SwfTz";
$dbname = "kguzy";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$getUserInfo = "SELECT currency from User WHERE username = '$current_username'";
$purse = $conn->query($getUserInfo);
$purse = $purse->fetch_assoc();
$purse = $purse["currency"];
?>
<p align="right"><strong><font size="+1"><?= $current_username?></strong> $<?= $purse ?> </font></p>
<?php
// Function to fetch data from the prizes table
function getPrizes($conn)
{
$sql = "SELECT * FROM Prizes";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
// Select specific attributes from the row
$prizes[] = [
'Prize_ID' => $row['Prize_ID'],
'name' => $row['name'],
'cost' => $row['cost'],
];
}
return $prizes;
} else {
echo "Error executing query: " . $conn->error;
return [];
}
}
// Function to get the user's current credits
function getUserCredits($conn, $current_username)
{
//$credits = 0;
//$stmt = $conn->prepare("SELECT currency FROM User WHERE username = '$current_username'");
//$stmt->execute();
//$result = $stmt->get_result();
$credits = $conn->query("SELECT currency FROM User WHERE username = '$current_username'");
$credits = $credits->fetch_assoc();
$credits = $credits['currency'];
/*if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$credits = $row['currency'];
}*/
//$stmt->close();
return $credits;
}
// Read data from the database (using only the prizes table)
$prizes = getPrizes($conn);
// Display order page
echo '<h1>Order Page</h1>';
// Process order form submission
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["prizes"])) {
//echo "In form processing ";
// Get the user ID
$user_id_query = "SELECT User_ID FROM User WHERE username = ?";
$user_id_stmt = $conn->prepare($user_id_query);
$user_id_stmt->bind_param("s", $current_username);
if(!$user_id_stmt->execute()){
echo "Order form error description: " . $conn->error;
} //else { echo "Order form ran<br>";}
$user_id_result = $user_id_stmt->get_result();
$user_id_row = $user_id_result->fetch_assoc();
$user_id = $user_id_row["User_ID"];
// Get the user's current credits
$current_credits = getUserCredits($conn, $current_username);
//echo "got credits: $current_credits <br>";
// Calculate the total cost of selected prizes
$total_cost = 0;
foreach ($_POST["prizes"] as $prize_id) {
foreach ($prizes as $prize) {
if ($prize["Prize_ID"] == $prize_id) {
$total_cost += $prize["cost"];
//break;
}
}
}
//echo "Total: $total_cost";
// Check if the user has sufficient credits
if ($current_credits >= $total_cost) {
// Subtract the cost from the user's credits
// Updates User table
$new_credits = $current_credits - $total_cost;
$update_credits_query = "UPDATE User SET currency = ? WHERE User_ID = ?";
$update_credits_stmt = $conn->prepare($update_credits_query);
$update_credits_stmt->bind_param("ii", $new_credits, $user_id);
//echo "credits updated";
if(!$update_credits_stmt->execute()){
echo "Credit check error: " . $conn->error;
}
// Insert orders into the Orders table
$insert_order_query = "INSERT INTO Orders (Order_ID, User_ID, Prize_ID) VALUES (?, ?, ?)";
$insert_order_stmt = $conn->prepare($insert_order_query);
$orderID;
foreach ($_POST["prizes"] as $prize_id) {
$insert_order_stmt->bind_param("iii", $orderID, $user_id, $prize_id);
if(!$insert_order_stmt->execute()){
echo "INSERT INTO Orders error description: " . $conn->error;
}
}
// Close the prepared statements
$update_credits_stmt->close();
$insert_order_stmt->close();
// Redirect to intermediate page after processing
echo '<script>window.location.href="orderPlaced.php"</script>';
} else {
// User doesn't have sufficient credits, display an error message or handle it accordingly
echo '<script>alert("Insufficient credits to place the order.");</script>';
}
// Close the user ID statement
$user_id_stmt->close();
}
echo '<form action="order_page.php" method="post">'; // Assuming you will process the order on the same page
echo '<table>';
echo '<tr><th>Name</th><th>Cost</th><th>Select</th></tr>';
foreach ($prizes as $prize) {
// Display prize details with checkboxes
echo '<tr>';
echo '<td>' . $prize['name'] . '</td>';
echo '<td>' . $prize['cost'] . '</td>';
echo '<td><input type="checkbox" name="prizes[]" value="' . $prize['Prize_ID'] . '"></td>';
echo '</tr>';
}
echo '</table>';
echo '<div>';
echo '<button type="submit" action="placeOrder()">Place Order</button>';
echo "<button onclick='cancelOrder()'>Cancel Order</button>";
echo '</div>';
echo '</form>';
// Close the database connection
$conn->close();
?>
<!-- Popups
<div class="popup" onclick='closePopup("placedPopup");'>Click to close
<span class="popuptext" id="placedPopup">Order successfully placed!</span>
</div>
<div class="popup" onclick='closePopup("cancelledPopup");'>Click to close
<span class="popuptext" id="cancelledPopup">Order cancelled</span>
</div>-->
</body>
</html>