This repository has been archived by the owner on Mar 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPurchaseBook.php
373 lines (302 loc) · 12 KB
/
PurchaseBook.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
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
<?php
session_start();
$isAuthor = $_SESSION['isAuthor'];
$isAdmin = $_SESSION['isAdmin'];
include "NavBar.php";
include "helper.php";
navBar($isAdmin, $isAuthor);
//$user_id = 1; // this will come from session array, once it is connected to rest of the pages
$user_id = $_SESSION['user_id'];
$conn = getDatabaseConnection();
$filtered = false;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ebook Purchase</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./styles.css">
</head>
<body>
<h2>Welcome to The Ebook Purchase Page</h2>
<!-- filtering buttons-->
<form action="" method="post">
<div class="container">
<h1>Filter Ebooks</h1>
<p>Please fill in this form to filter ebooks.</p>
<hr>
<label for="title"><b>Title</b></label>
<input type="text" placeholder="Enter Title" name="title">
<label for="genre"><b>Genre</b></label>
<input type="text" placeholder="Enter Genre" name="genre" >
<label for="author"><b>Author</b></label>
<input type="text" placeholder="Enter Author" name="author" >
<label for="publisher"><b>Publisher</b></label>
<input type="text" placeholder="Enter Publisher" name="publisher" >
<!--price range-->
<label for="min_price"><b>Min Price</b></label>
<input type="number" min="1" max="1000000" name="min_price" >
<label for="max_price"><b>Max Price</b></label>
<input type="number" min="1" max="1000000" name="max_price">
<input type="submit" value="Filter" name="filter">
</div>
</form>
<?php
if (isset($_POST['filter'])) {
// echo "filter button was pressed";
// check if fields are empty
if (empty($_POST['genre']) && empty($_POST['author']) && empty($_POST['publisher'])
&& empty($_POST['min_price']) && empty($_POST['max_price']) && empty($_POST['title'])) {
// echo "Please fill in at least one field";
$books = array();
} else {
// get data from form
$books_genre = array();
$books_author = array();
$books_publisher = array();
$books_price = array();
$books_title = array();
if (!empty($_POST['genre'])) {
$genre = $_POST['genre'];
// search for genre using like
$sql = "SELECT * FROM genre WHERE genre_name LIKE '%$genre%'";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
$genre_id = $row['genre_id'];
// echo "genre id is $genre_id";
$sql2 = "SELECT * FROM book_genre WHERE genre_id = $genre_id";
$result2 = $conn->query($sql2);
while ($row2 = $result2->fetch_assoc()) {
$book_id = $row2['book_id'];
// echo "book id is $book_id";
array_push($books_genre, $book_id);
}
}
// echo "books genre";
// print_r($books_genre);
// echo "<br>";
}
if (!empty($_POST['title'])) {
$title = $_POST['title'];
// echo "title is \"$title\"";
// search for title using like
$sql = "SELECT * FROM book WHERE title LIKE '%$title%'";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
$book_id = $row['book_id'];
array_push($books_title, $book_id);
}
//
// echo "books title";
// print_r($books_title);
// echo "<br>";
}
if (!empty($_POST['author'])) {
$author = $_POST['author'];
// search for author using like
$sql = "SELECT * FROM sys_author natural join author_publish_ebook WHERE first_name LIKE '%$author%' OR last_name LIKE '%$author%'";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
$book_id = $row['book_id'];
array_push($books_author, $book_id);
}
// echo "books author";
// print_r($books_author);
// echo "<br>";
}
if (!empty($_POST['publisher'])) {
$publisher = $_POST['publisher'];
// search for publisher using like
$sql = "SELECT * FROM publisher natural join book WHERE publisher_name LIKE '%$publisher%'";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
$book_id = $row['book_id'];
array_push($books_publisher, $book_id);
}
// echo "books publisher";
// print_r($books_publisher);
// echo "<br>";
}
if (!empty($_POST['min_price']) && !empty($_POST['max_price'])) {
$min_price = $_POST['min_price'];
$max_price = $_POST['max_price'];
// echo "min price is $min_price and max price is $max_price";
// search for price using between
$sql = "SELECT * FROM e_book WHERE price BETWEEN $min_price AND $max_price";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
$book_id = $row['book_id'];
array_push($books_price, $book_id);
}
// echo "books price";
// print_r($books_price);
// echo "<br>";
}
// get the intersection of all the arrays if they are not empty
$books = array();
if (!empty($_POST['genre'])) {
if ($books) {
$books = array_intersect($books, $books_genre);
} else {
$books = $books_genre;
}
}
if (!empty($_POST['author'])) {
if ($books) {
$books = array_intersect($books, $books_author);
} else {
$books = $books_author;
}
}
if (!empty($_POST['publisher'])) {
if ($books) {
$books = array_intersect($books, $books_publisher);
} else {
$books = $books_publisher;
}
}
if (!empty($_POST['min_price']) && !empty($_POST['max_price'])) {
if ($books) {
$books = array_intersect($books, $books_price);
} else {
$books = $books_price;
}
}
if (!empty($_POST['title'])) {
if ($books) {
$books = array_intersect($books, $books_title);
} else {
$books = $books_title;
}
}
// echo "books";
// print_r($books);
// echo "<br>";
$filtered = true;
}
}
?>
<?php
// list all ebooks
if (!$filtered) {
$sql = "SELECT * FROM e_book";
} else if ($filtered && !empty($books)) {
$sql = "SELECT * FROM e_book WHERE book_id IN (" . implode(',', $books) . ")";
} else {
$sql = "SELECT * FROM e_book WHERE book_id = -1";
}
//echo $sql;
$result = $conn->query($sql);
echo "<h1>Ebooks Not Purchased by You</h1>";
echo "<p> Select a book to purchase</p>";
// create table with buy button for each ebook
if ($result->num_rows > 0) {
echo "<form id='ebook_form' method='post'>";
// $row = $result->fetch_assoc();
// print_r($row);
// echo "<table><tr><th>Ebook Name</th><th>Author Name</th><th>Genre Name</th><th> PublisherName</th><th>PublishDate</th>
// <th>Price</th><th>Buy</th></tr>";
echo "<select name='e_books' id='e_books_selection_id' multiple>";
while ($row = $result->fetch_assoc()) {
$book_id = $row['book_id'];
// find books properties from the book id
// echo "book id is $book_id";
// check if the book is already purchased by the user
$sql2 = "SELECT * FROM purchase WHERE user_id = $user_id AND book_id = $book_id";
$result2 = $conn->query($sql2);
if ($result2->num_rows == 0) {
$book_info = getEBookInfo( $book_id, $conn );
// echo "book info";
// print_r($book_info);
echo "<option value='$book_id'>" . $book_info['book_title'] . ", " . $book_info['author_name'] . ", " .$book_info["price"] . " TL" ."</option>";
}
}
echo "</select>";
} else {
echo "<h2>No ebooks found :(</h2>";
}
?>
<?php
// list users all credit cards
$sql = "SELECT * FROM user_has_credit_card WHERE user_id = $user_id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<h1>Your Credit Cards</h1>";
echo "<p> Choose a credit card to purchase the ebook</p>";
echo "<select name='credit_cards' id='credit_cards_selection_id' multiple>";
while ($row = $result->fetch_assoc()) {
$card_id = $row['card_id'];
// find credit card properties from the card id
$sql2 = "SELECT * FROM credit_card WHERE card_id = $card_id";
$result2 = $conn->query($sql2);
$row2 = $result2->fetch_assoc();
$card_number = $row2['card_number'];
$card_type = $row2['card_type'];
$name_on_card = $row2['name_on_card'];
echo "<option value='$card_id'>" . $card_number . ", " . $card_type . ", " . $name_on_card . "</option>";
}
echo "</select>";
echo "<input type='submit' value='Buy' name='buy'>";
echo "</form>";
} else {
echo "You have no credit cards :( Please add a credit card. <br>";
}
?>
</body>
</html>
<?php
// get data from submit button
if(isset($_POST['buy'])) {
// echo "buy button was pressed";
if (isset($_POST['e_books']) && isset($_POST['credit_cards'])) {
// get data from form
$ebook_id = $_POST['e_books'];
$card_id = $_POST['credit_cards'];
// unset post
unset($_POST['e_books']);
unset($_POST['credit_cards']);
// echo "ebook id is $ebook_id" . "credit card id is $card_id";
// check if the user has enough credit to buy the book
$sql = "SELECT * FROM e_book WHERE book_id = $ebook_id";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$price = $row['price'];
$sql = "SELECT * FROM credit_card WHERE card_id = $card_id";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$credit = $row['balance'];
if ($credit < $price) {
// make alert that the user does not have enough credit
echo "<script type='text/javascript'>alert('You do not have enough credit to buy this book.');</script>";
} else {
// insert into purchase table
$sql = "INSERT INTO purchase (user_id, card_id, book_id, date) VALUES ($user_id, $card_id, $ebook_id, NOW())";
$result2 = $conn->query($sql);
// if (!$result) {
// echo "<script type='text/javascript'>alert('There was an error while purchasing the book.');</script>";
// // go back to the page
// header("Refresh:0");
// }
// update credit card balance
$new_credit = $credit - $price;
// echo "new credit is $new_credit, old credit is $credit, price is $price";
$sql = "UPDATE credit_card SET balance = $new_credit WHERE card_id = $card_id";
$result = $conn->query($sql);
// check the result
if ($result && $result2) {
// make alert that the purchase was successful
echo "<script type='text/javascript'>alert('Purchase was successful.');</script>";
} else {
// make alert that the purchase was not successful
echo "<script type='text/javascript'>alert('There was an error while purchasing the book.');</script>";
}
}
} else {
// make alert that the user did not select a book or credit card
echo "<script type='text/javascript'>alert('You did not select a book or credit card.');</script>";
}
// clear post
header("Refresh:0");
}
?>