forked from DizonAj16/alimbrary_v2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbooks.php
366 lines (325 loc) · 17.1 KB
/
books.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
<?php
// Include config file
require_once "config.php";
// Initialize variables
$title = $author = $isbn = $pub_year = $genre = "";
$title_err = $author_err = $isbn_err = $pub_year_err = $genre_err = $image_err = "";
// Processing form data when form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Validate and retrieve form data
$title = trim($_POST["title"]);
$author = trim($_POST["author"]);
$isbn = trim($_POST["isbn"]);
$pub_year = trim($_POST["pub_year"]);
$genre = trim($_POST["genre"]);
// Generate and check ISBN
do {
// Generate a random number for the first part of the ISBN (9 digits)
$random_number = mt_rand(100000000, 999999999);
// Format the random number to match ISBN format (###-##########)
$isbn = "978-" . $random_number;
// Check if the generated ISBN already exists in the database
$sql_check_isbn = "SELECT * FROM books WHERE isbn = ?";
$stmt_check_isbn = mysqli_prepare($conn, $sql_check_isbn);
mysqli_stmt_bind_param($stmt_check_isbn, "s", $isbn);
mysqli_stmt_execute($stmt_check_isbn);
$result_check_isbn = mysqli_stmt_get_result($stmt_check_isbn);
$num_rows = mysqli_num_rows($result_check_isbn);
} while ($num_rows > 0);
// Validate title, author, isbn, publication year, and genre (you already have this code)
// Check if file is uploaded without errors
if (isset($_FILES["image"]) && $_FILES["image"]["error"] == 0) {
// Process and move the uploaded file
$target_dir = "uploads/"; // Directory where uploaded files will be stored
$target_file = $target_dir . basename($_FILES["image"]["name"]); // Path of the uploaded file
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION)); // File extension
// Check if the file is an actual image or fake image
$check = getimagesize($_FILES["image"]["tmp_name"]);
if ($check !== false) {
// Allow certain file formats
if ($imageFileType == "jpg" || $imageFileType == "png" || $imageFileType == "jpeg" || $imageFileType == "gif") {
// Move the uploaded file to the specified directory
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
// File uploaded successfully, now insert book data into the database
$image_path = $target_file; // Get image path
} else {
$image_err = "Sorry, there was an error uploading your file.";
}
} else {
$image_err = "Sorry, only JPG, JPEG, PNG, and GIF files are allowed.";
}
} else {
$image_err = "File is not an image.";
}
} else {
$image_err = "No image file uploaded.";
}
// Free the result set and close the statement
mysqli_free_result($result_check_isbn);
mysqli_stmt_close($stmt_check_isbn);
// Check input errors before inserting into database
if (empty($title_err) && empty($author_err) && empty($isbn_err) && empty($pub_year_err) && empty($genre_err) && empty($image_err)) {
// Check if any of the required fields are empty
if (empty($title) || empty($author) || empty($isbn) || empty($pub_year) || empty($genre) || empty($image_path)) {
// Redirect to landing page or any other appropriate action
header("location: books.php");
exit();
}
// Prepare an insert statement
$sql = "INSERT INTO books (title, author, isbn, pub_year, genre, image_path) VALUES (?, ?, ?, ?, ?, ?)";
if ($stmt = mysqli_prepare($conn, $sql)) {
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ssssss", $param_title, $param_author, $param_isbn, $param_pub_year, $param_genre, $param_image_path);
// Set parameters
$param_title = $title;
$param_author = $author;
$param_isbn = $isbn;
$param_pub_year = $pub_year;
$param_genre = $genre;
$param_image_path = $image_path;
// Attempt to execute the prepared statement
if (mysqli_stmt_execute($stmt)) {
// Redirect to landing page
header("location: books.php");
exit();
} else {
echo "Oops! Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Books section</title>
<!-- Include Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<!-- Include Font Awesome -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
/* Fixed position for the header container */
.header-container {
position: fixed;
top: 0;
left: 0;
right: 0;
background-color: #fff;
/* Adjust background color as needed */
z-index: 1000;
/* Ensure the header is above other content */
}
/* Margin to push content below the fixed header */
body {
margin-top: 70px;
/* Adjust as needed based on header height */
}
/* Adjustments for the back-to-top button */
#backToTopBtn {
display: none;
position: fixed;
bottom: 20px;
right: 20px;
z-index: 99;
font-size: 18px;
border: none;
outline: none;
background-color: rgba(0, 0, 0, 0.5);
color: white;
cursor: pointer;
border-radius: 50%;
}
#backToTopBtn:hover {
background-color: rgba(0, 0, 0, 0.7);
}
</style>
</head>
<body>
<div class="header-container bg-light">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="mt-3 clearfix">
<h2 class="pull-left">Books</h2>
<a href="welcome.php" class="text-light">
<button class="btn btn-outline-dark btn-md pull-right disabled" data-toggle="tooltip" data-placement="top" title="Back to Dashboard">
<i class="fa fa-arrow-left" style="color: black;"></i>
</button>
</a>
<button type="button" class="btn btn-success btn-md pull-right mr-2" data-toggle="modal" data-target="#exampleModal" data-toggle="tooltip" data-placement="top" title="Add New Book">
<i class="fa fa-plus-circle"></i>
</button>
<button class="btn btn-secondary btn-md pull-right mr-2" type="button" id="refreshButton" data-toggle="tooltip" data-placement="top" title="Refresh">
<i class="fa fa-refresh"></i>
</button>
<button class="btn btn-outline-info btn-md pull-right mr-2" type="button" id="searchButton" data-toggle="tooltip" data-placement="top" title="Search">
<i class="fa fa-search"></i>
</button>
<input type="text" id="searchInput" class="form-control form-control-md pull-right mr-2" placeholder="Search books" style="width:200px;" data-toggle="tooltip" data-placement="top" title="Search books">
</div>
</div>
</div>
</div>
</div>
<div class="modal fade" id="exampleModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Create book</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" enctype="multipart/form-data">
<div class="form-group">
<label>Title</label>
<input type="text" name="title" class="form-control <?php echo (!empty($title_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $title; ?>">
<span class="invalid-feedback"><?php echo $title_err; ?></span>
</div>
<div class="form-group">
<label>Author</label>
<input type="text" name="author" class="form-control <?php echo (!empty($author_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $author; ?>">
<span class="invalid-feedback"><?php echo $author_err; ?></span>
</div>
<div class="form-group">
<label>ISBN</label>
<input type="text" name="isbn" class="form-control <?php echo (!empty($isbn_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $isbn; ?>">
<span class="invalid-feedback"><?php echo $isbn_err; ?></span>
</div>
<div class="form-group">
<label>Publication year</label>
<input type="text" name="pub_year" class="form-control <?php echo (!empty($pub_year_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $pub_year; ?>">
<span class="invalid-feedback"><?php echo $pub_year_err; ?></span>
</div>
<div class="form-group">
<label>Genre</label>
<input type="text" name="genre" class="form-control <?php echo (!empty($genre_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $genre; ?>">
<span class="invalid-feedback"><?php echo $genre_err; ?></span>
</div>
<div class="form-group">
<label>Image</label>
<input type="file" name="image" class="form-control-file">
<span class="invalid-feedback"><?php echo $image_err; ?></span>
</div>
<input type="submit" class="btn btn-primary" value="Submit">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</form>
</div>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="container">
<div class="row row-cols-1 row-cols-md-2 row-cols-lg-3">
<?php
// Include config file
require_once "config.php";
// Attempt select query execution
$sql = "SELECT * FROM books";
if ($result = mysqli_query($conn, $sql)) {
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
echo '<div class="col sm-2 mb-4">';
echo '<div class="card border-dark h-100">';
echo '<div class="d-flex justify-content-center align-items-center mt-2 "
style="
width: auto;
height: 220px;
">';
// Display the image if image path exists
if (!empty($row['image_path'])) {
echo '<img src="' . $row['image_path'] . '
"alt="Book Image"
style="
max-height: 200px;
max-width: 150px;
">';
} else {
echo '<span>No image available</span>';
}
echo '</div>';
echo '<div class="card-body d-flex flex-column" style="height: 300px;">';
echo '<h5 class="card-title text-center" style="height: 50px">' . $row['title'] . '</h5>';
echo '<p class="card-text text-center">Author: ' . $row['author'] . '</p>';
echo '<p class="card-text text-center">ISBN: ' . $row['isbn'] . '</p>';
echo '<p class="card-text text-center">Publication Year: ' . $row['pub_year'] . '</p>';
echo '<p class="card-text text-center">Genre: ' . $row['genre'] . '</p>';
echo '<div class="d-flex flex-row justify-content-center align-items-center bg-secondary rounded p-2 mx-auto" style="max-width: 120px;">';
echo '<a href="viewbook.php?book_id=' . $row['book_id'] . '" class="mr-3 text-light" title="View Record" data-toggle="tooltip"><span class="fa fa-eye fa-lg"></span></a>';
echo '<a href="updatebook.php?book_id=' . $row['book_id'] . '" class="mr-3 text-light" title="Update Record" data-toggle="tooltip"><span class="fa fa-pencil fa-lg"></span></a>';
echo '<a href="deletebook.php?book_id=' . $row['book_id'] . '" class="text-light" title="Delete Record" data-toggle="tooltip"><span class="fa fa-trash fa-lg"></span></a>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
}
// Free result set
mysqli_free_result($result);
} else {
echo '<div class="alert alert-danger"><em>No records were found.</em></div>';
}
} else {
echo "Oops! Something went wrong. Please try again later.";
}
// Close connection
mysqli_close($conn);
?>
</div>
</div>
</div>
</div>
<button id="backToTopBtn" title="Go to top" style="height: 50px; width:50px;"><i class="fa fa-arrow-up"></i></button>
<!-- Include Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
$(document).ready(function() {
$("#searchButton").click(function() {
var searchText = $("#searchInput").val().toLowerCase();
$(".card").each(function() {
var title = $(this).find(".card-title").text().toLowerCase();
var author = $(this).find(".card-text").eq(0).text().toLowerCase();
var isbn = $(this).find(".card-text").eq(1).text().toLowerCase();
var pubYear = $(this).find(".card-text").eq(2).text().toLowerCase();
var genre = $(this).find(".card-text").eq(3).text().toLowerCase();
if (title.indexOf(searchText) === -1 && author.indexOf(searchText) === -1 && isbn.indexOf(searchText) === -1 && pubYear.indexOf(searchText) === -1 && genre.indexOf(searchText) === -1) {
$(this).hide();
} else {
$(this).show();
}
});
});
// Refresh button click event
$("#refreshButton").click(function() {
location.reload(); // Reload the page
});
});
</script>
<script>
$(document).ready(function() {
// Show or hide the button based on scroll position
$(window).scroll(function() {
if ($(this).scrollTop() > 100) {
$('#backToTopBtn').fadeIn();
} else {
$('#backToTopBtn').fadeOut();
}
});
// Scroll to top when button is clicked
$('#backToTopBtn').click(function() {
$('html, body').animate({
scrollTop: 0
}, 'slow');
return false;
});
});
</script>
</body>
</html>