-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.php
283 lines (246 loc) · 11.3 KB
/
index.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
<?php
session_start();
require 'E-Library/db_connect.php';
// Check if it's an AJAX request for loading more books
if (isset($_GET['section']) && isset($_GET['limit']) && isset($_GET['offset'])) {
$section = $_GET['section'];
$limit = intval($_GET['limit']);
$offset = intval($_GET['offset']);
if ($section === 'featured') {
$stmt = $connection->prepare("SELECT book_id, TITLE, AUTHOR, `BOOK_COVER` AS IMAGE FROM books ORDER BY book_id ASC LIMIT ? OFFSET ?");
} elseif ($section === 'most-visited') {
$stmt = $connection->prepare("SELECT book_id, TITLE, AUTHOR, `BOOK_COVER` AS IMAGE FROM books WHERE visits >= 20 ORDER BY visits DESC LIMIT ? OFFSET ?");
} else {
echo json_encode([]);
exit;
}
$stmt->bind_param("ii", $limit, $offset);
$stmt->execute();
$result = $stmt->get_result();
// Fetch books and return them as JSON
$books = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode($books);
exit;
}
// Check if a specific book's details page is requested (for visit tracking)
if (isset($_GET['book_id'])) {
$book_id = intval($_GET['book_id']);
// Increment the visits count for the book
$title = "UPDATE BOOKS SET visits = visits + 1 WHERE book_id = $book_id";
$connection->query($title);
// Fetch the book details
$title = "SELECT * FROM BOOKS WHERE book_id = $book_id";
$result = $connection->query($title);
if ($result->num_rows > 0) {
$book = $result->fetch_assoc();
// Display book details
echo "<h1>" . htmlspecialchars($book['TITLE']) . "</h1>";
echo "<p>by " . htmlspecialchars($book['AUTHOR']) . "</p>";
echo "<img src='db/Book_Covers/" . htmlspecialchars($book['BOOK COVER']) . "' alt='" . htmlspecialchars($book['TITLE']) . " book cover'>";
echo "<p>Visits: " . $book['visits'] . "</p>";
exit;
} else {
echo "Book not found.";
exit;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Library</title>
<link rel="icon" type="image/x-icon" href="E-Library/img/favicon/sk.ico">
<link rel="stylesheet" href="E-Library/css/styles1.css?v=<?php echo time(); ?>">
<link rel="stylesheet" href="E-Library/css/header.css?v=<?php time(); ?>">
<link rel="stylesheet" href="E-Library/css/footer.css?v=<?php echo time(); ?>">
<style>
.star-rating {
display: flex;
justify-content: center;
margin: 10px 0;
}
.star {
cursor: pointer;
color: white; /* Default color for unselected stars */
font-size: 30px; /* Adjust size as needed */
}
.star.selected {
color: gold; /* Color for selected stars */
}
</style>
</head>
<body>
<?php
include 'E-Library/header.php';
?>
<!-- Search Bar -->
<div class="search-bar">
<div>
<form id="form">
<div class="sb">
<input type="text" name="title" id="searchInput" placeholder="Search for books, authors, or genres...">
<input type="text" name="author" placeholder="Author">
<select name="genre" class="select-option">
<option value="">Category</option>
<?php
$cat = $connection->prepare("SELECT DISTINCT CATEGORY FROM books");
$cat->execute();
$dog = $cat->get_result();
?>
<?php while ($rat = $dog->fetch_assoc()): ?>
<option value="<?php echo htmlspecialchars($rat['CATEGORY']); ?>">
<?php echo htmlspecialchars($rat['CATEGORY']); ?>
</option>
<?php endwhile; ?>
</select>
</div>
<div style="padding-top: 20px; justify-content: center;" class="sb"><input type="submit" value="Search" class="submit-btn"></div>
</form>
</div>
</div>
<h3 id="h3">Search Results</h3>
<div class="search-results" id="rs">
<div class="book-list" id="results"></div>
</div>
<!-- Featured Books Section -->
<div class="featured-section" id="fb">
<h2>Featured Books</h2>
<div class="book-list" id="featured-books">
<?php
$title = 'SELECT book_id, TITLE, AUTHOR, BOOK_COVER AS IMAGE FROM BOOKS ORDER BY book_id ASC LIMIT 10';
$result = $connection->query($title);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo '<div class="book-item">';
echo '<div class="book-coverpage">';
echo '<a href="E-Library/bookinfo.php?book_id=' . $row['book_id'] . '"><img src="E-Library/img/Book_Covers/' . htmlspecialchars($row['IMAGE']) . '" alt="' . htmlspecialchars($row['TITLE']) . ' book cover image"></a>';
echo '</div>';
echo '<div class="book-title">';
echo '<a class="title-anchor" href="E-Library/bookinfo.php?book_id=' . $row['book_id'] . '">' . htmlspecialchars($row['TITLE']) . '</a>';
echo '</div>';
echo '</div>';
}
} else {
echo '<p>No books found</p>';
}
?>
</div>
<button class="show-more-btn" onclick="loadMoreBooks('featured')">Show More</button>
</div>
<!-- Most Visited Books Section -->
<div class="most-visited-section" id="msv">
<h2>Most Visited</h2>
<div class="book-list" id="most-visited-books">
<?php
$title = 'SELECT book_id, TITLE, AUTHOR, `BOOK_COVER` AS IMAGE FROM BOOKS WHERE visits > 0 ORDER BY visits DESC LIMIT 10';
$result = $connection->query($title);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo '<div class="book-item">';
echo '<div class="book-coverpage">';
echo '<a href="E-Library/bookinfo.php?book_id=' . $row['book_id'] . '"><img src="E-Library/img/Book_Covers/' . htmlspecialchars($row['IMAGE']) . '" alt="' . htmlspecialchars($row['TITLE']) . ' book cover image"></a>';
echo '</div>';
echo '<div class="book-title">';
echo '<a class="title-anchor" href="E-Library/bookinfo.php?book_id=' . $row['book_id'] . '">' . htmlspecialchars($row['TITLE']) . '</a>';
echo '</div>';
echo '</div>';
}
} else {
echo '<p>No books found</p>';
}
?>
</div>
<button class="show-more-btn" onclick="loadMoreBooks('most-visited')">Show More</button>
</div>
<!-- Star Rating Section -->
<div class="star-rating-section" id="star-rating" style="text-align: center; margin: 20px 0;">
<h2 style="color: black;">Rate your user experience for this website</h2>
<div class="star-rating" style="font-size: 24px;">
<span class="star" data-value="1">★</span>
<span class="star" data-value="2">★</span>
<span class="star" data-value="3">★</span>
<span class="star" data-value="4">★</span>
<span class="star" data-value="5">★</span>
</div>
<!-- Review Section -->
<textarea id="user-review" rows="4" cols="50" placeholder="Type your review here..."></textarea>
<br>
<button id="submit-review" class="submit-btn">Submit Review</button>
</div>
<!-- Footer -->
<?php include 'E-Library/footer.php'; ?>
<script src="E-Library/Script_Functions.js"></script>
<script>
document.getElementById('form').addEventListener('submit', async function(event) {
event.preventDefault();
const formData = new FormData(event.target);
const response = await fetch('E-Library/searchResult.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams(formData)
});
document.getElementById('results').innerHTML = '';
const data = await response.json();
console.log(data);
// Hide the other sections and display only the results section
document.getElementById('fb').style.display = 'none';
document.getElementById('msv').style.display = 'none';
document.getElementById('rs').style.display = 'block';
document.getElementById('h3').style.display = 'block';
if (data.message) {
document.getElementById('results').innerHTML = data.message
} else {
data.forEach(book => {
const bookDiv = document.createElement('div');
bookDiv.classList.add('book-item');
const coverPage = document.createElement('div');
coverPage.classList.add('book-coverpage');
const coverImage = document.createElement('img');
coverImage.src = 'E-Library/img/Book_Covers/' + book.IMAGE;
coverImage.alt = book.TITLE + 'book cover image';
coverPage.appendChild(coverImage);
bookDiv.appendChild(coverPage);
const titleDiv = document.createElement('div');
titleDiv.classList.add('book-title');
const titleLink = document.createElement('a');
titleLink.href = 'E-Library/bookinfo.php?book_id=' + book.book_id;
titleLink.textContent = book.TITLE;
titleDiv.appendChild(titleLink);
bookDiv.appendChild(titleDiv);
document.getElementById('results').appendChild(bookDiv);
});
}
});
window.onload = function() {
document.getElementById('rs').style.display = 'none';
document.getElementById('h3').style.display = 'none';
};
// Star rating functionality
const stars = document.querySelectorAll('.star');
let selectedRating = 0;
stars.forEach(star => {
star.addEventListener('click', () => {
selectedRating = star.getAttribute('data-value');
stars.forEach(s => {
s.classList.remove('selected');
});
for (let i = 0; i < selectedRating; i++) {
stars[i].classList.add('selected');
}
});
});
document.getElementById('submit-rating').addEventListener('click', () => {
if (selectedRating > 0) {
document.getElementById('rating-message').textContent = `You rated this book ${selectedRating} star(s)!`;
// Here you can add code to send the rating to the server
} else {
document.getElementById('rating-message').textContent = 'Please select a rating.';
}
});
</script>
</body>
</html>