source: BookView.php

main
Last change on this file was 75f74d9, checked in by Vlado 222039 <vlado.popovski@…>, 6 weeks ago

Initial commit: Adding Book Tracker code

  • Property mode set to 100644
File size: 10.1 KB
Line 
1<?php
2session_start();
3
4require 'connect.php';
5
6if (isset($_GET['bookid'])) {
7
8 $param = $_GET['bookid'];
9
10 //π_{book., book_details., firstname, lastname, ...} (σ_{book.bookid = :bookid } (book ⨝ book_author ⨝ author ⨝ book_details))
11 //QUERY TREE?
12 $sql = "SELECT book.*, book_details.*, firstname, lastname, author_description, author_image FROM book
13 INNER JOIN book_author ON book.bookid = book_author.bookid
14 INNER JOIN author ON book_author.authorid = author.authorid
15 INNER JOIN book_details ON book.bookid = book_details.bookid
16 WHERE book.bookid = :bookid;
17 ";
18 $stmt = $conn->prepare($sql);
19 $stmt->bindParam(':bookid', $param, PDO::PARAM_INT);
20
21 //π_{BookID, TotalCopies - COUNT(LoanID)} (σ_{BookID = :bookid } (Book ⨝ (Loan ⨝ (π_{CopyID} (σ_{BookID = b.BookID} Book_Copies)))))
22 $sql2 = "SELECT
23 b.BookID,
24 b.TotalCopies - COALESCE(COUNT(l.LoanID), 0) AS AvailableCopies
25 FROM Book b
26 LEFT JOIN Loan l
27 ON l.BookCopyID IN (SELECT CopyID FROM Book_Copies WHERE BookID = b.BookID)
28 AND l.Status IN ('On Time', 'Soon', 'Overdue')
29 WHERE
30 b.BookID = :bookid
31 GROUP BY
32 b.BookID, b.TotalCopies;";
33
34 $stmt->execute();
35 $result = $stmt->fetch(PDO::FETCH_ASSOC);
36
37
38 if ($result) {
39
40 $bookid = $result['bookid'];
41 $book_name = $result['title'];
42 $book_isbn = $result['isbn'];
43 $book_genre = $result['genre'];
44 $book_year = $result['publishedyear'];
45 $book_image = $result['coverimage'];
46 $book_copies = $result['totalcopies'];
47 $book_description = $result['description'];
48 $author_name = $result['firstname'] . ' ' . $result['lastname'];
49 $author_bio = $result['author_description'];
50 $author_image = $result['author_image'];
51 $book_format = $result['format'];
52 $book_language = $result['language'];
53 $book_publisher = $result['publisher'];
54 $book_pages = $result['pages'];
55 }
56 else {
57 // ERROR HANDLING
58 }
59
60
61 $stmt = $conn->prepare($sql2);
62 $stmt->bindParam(':bookid', $param, PDO::PARAM_INT);
63 // Execute the query
64 $stmt->execute();
65 $result = $stmt->fetch(PDO::FETCH_ASSOC);
66
67 if ($result) {
68 $book_available = $result['availablecopies'];
69 }
70 else {
71 // ERROR HANDLING
72 }
73
74 //Similar Books
75 //π_{bookid, coverimage, title, genre, firstname, lastname} (σ_{genre = :bookgenre ∧ bookid ≠ :bookid } (book ⨝ book_author ⨝ author))
76 $stmt = $conn->prepare("SELECT book.bookid, coverimage, title, genre, firstname, lastname FROM book
77 INNER JOIN book_author ON book.bookid = book_author.bookid
78 INNER JOIN author ON book_author.authorid = author.authorid
79 WHERE book.genre = :bookgenre AND book.bookid != :bookid LIMIT 5;
80 ");
81 $stmt->bindParam(':bookgenre', $book_genre, PDO::PARAM_STR);
82 $stmt->bindParam(':bookid', $bookid, PDO::PARAM_INT);
83 // Execute the query
84 $stmt->execute();
85 $similar_books = [];
86
87 while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
88 $similar_books[] = $row;
89 }
90
91 //More Author
92 //π_{bookid, coverimage, title, genre, firstname, lastname} (σ_{authorid IN (π_{authorid} (σ_{bookid = :bookid } book_author)) ∧ bookid ≠ :bookid } (book ⨝ book_author ⨝ author))
93 $stmt = $conn->prepare("SELECT book.bookid, coverimage, title, genre, firstname, lastname
94 FROM book
95 INNER JOIN book_author ON book.bookid = book_author.bookid
96 INNER JOIN author ON book_author.authorid = author.authorid
97 WHERE book_author.authorid IN (
98 SELECT book_author.authorid
99 FROM book_author
100 WHERE book_author.bookid = :bookid
101 ) AND book.bookid != :bookid
102 LIMIT 5;
103 ");
104 $stmt->bindParam(':bookid', $bookid, PDO::PARAM_INT);
105
106 // Execute the query
107 $stmt->execute();
108 $author_books = [];
109
110 while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
111 $author_books[] = $row;
112 }
113
114
115}
116
117?>
118
119<!DOCTYPE html>
120<html lang="en">
121<head>
122 <meta charset="UTF-8">
123 <meta name="viewport" content="width=device-width, initial-scale=1.0">
124 <title>Book Details</title>
125 <link rel="stylesheet" href="CSS/BookView.css">
126</head>
127<body>
128<?php include 'Components/Header.html'; ?>
129
130 <div class="container">
131 <div class="breadcrumb">
132 <a href="./HomePage.php">Home</a> /
133 <a href="./Books.php?">Books</a> /
134 <a href="./Books.php?genres[]=<?php echo $book_genre ?>"> <?php echo $book_genre ?></a> /
135 <?php echo $book_name ?>
136 </div>
137
138
139 <div class="book-details">
140 <div class="book-image">
141 <img src="./BookImages/<?php echo $book_image?>" alt="Book Cover" class="book-cover">
142 </div>
143 <div class="book-info">
144 <h1><?php echo $book_name ?></h1>
145 <div class="book-meta">
146 <p>By <?php echo $author_name ?> | Published: <?php echo $book_year ?></p>
147 <p>Genre: <?php echo $book_genre ?></p>
148 <p>ISBN: <?php echo $book_isbn ?></p>
149 </div>
150 <div class="availability available">
151 Available (<?php echo $book_available ?> copies)
152 </div>
153 <div class="actions">
154 <form action="./Books.php" method="GET">
155 <input type="hidden" name="bookid" value="<?php echo $bookid ?>" >
156 <button class="btn btn-primary" type="submit" value="add-to-cart" name="submit">Add to cart</button>
157 </form>
158 </div>
159 <div class="book-description">
160 <h2 class="section-title">Description</h2>
161 <p><?php echo $book_description ?></p>
162 </div>
163 <div class="book-details-meta">
164 <h2 class="section-title">Details</h2>
165 <p><strong>Format: </strong><?php echo $book_format ?></p>
166 <p><strong>Language: </strong><?php echo $book_language ?></p>
167 <p><strong>Publisher: </strong><?php echo $book_publisher ?></p>
168 <p><strong>Pages: </strong><?php echo $book_pages ?></p>
169 </div>
170 </div>
171 </div>
172
173 <div class="author-section">
174 <h2 class="section-title">About the Author</h2>
175 <div class="author-info">
176 <img src="./AuthorImages/<?php echo $author_image ?>" alt="Author Photo" class="author-photo">
177 <div>
178 <h3><?php echo $author_name ?></h3>
179 <p><?php echo $author_bio ?></p>
180 </div>
181 </div>
182 </div>
183
184 <div class="recommendations">
185 <h2 class="section-title">You May Also Like</h2>
186 <div class="book-grid" id="recommendations">
187 <!-- Recommendations will be populated by JavaScript -->
188 </div>
189 </div>
190
191 <div class="recommendations-author">
192 <h2 class="section-title">More From The Author</h2>
193 <div class="book-grid" id="recommendations-author">
194 <!-- Recommendations will be populated by JavaScript -->
195 </div>
196 </div>
197 </div>
198
199 <script>
200 const recommendedBooks = [
201 <?php
202 foreach($similar_books as $book) {
203 echo "{title: '" . $book['title'] . "',
204 author: '" . $book['firstname'] . " " . $book['lastname'] . "',
205 image: '" . $book['coverimage'] . "',
206 genre: '" . $book['genre'] . "',
207 bookid: '" . $book['bookid'] . "'},";
208 }
209 ?>
210 ];
211
212 const authorBooks = [
213 <?php
214 foreach($author_books as $author_book) {
215 echo "{title: '" . $author_book['title'] . "',
216 author: '" . $author_book['firstname'] . " " . $author_book['lastname'] . "',
217 image: '" . $author_book['coverimage'] . "',
218 genre: '" . $author_book['genre'] . "',
219 bookid: '" . $author_book['bookid'] . "'},";
220 }
221 ?>
222 ];
223
224 // Function to populate recommended books
225 function populateRecommendations() {
226 const recommendationsContainer = document.getElementById('recommendations');
227
228 recommendedBooks.forEach(book => {
229 const bookCard = document.createElement('div');
230 bookCard.className = 'book-card';
231
232 bookCard.innerHTML = `
233 <img src="./BookImages/${book.image}" alt="${book.title}">
234 <a href='./BookView.php?bookid=${book.bookid}' style="text-decoration: none"><h3 class="book-title" style="color: green;">${book.title}</h3></a>
235 <p>${book.author}</p>
236 <p>${book.genre}</p>
237 `;
238
239 recommendationsContainer.appendChild(bookCard);
240 });
241 }
242
243 function populateMoreAuthor(){
244 const recommendationsAuthorContainer = document.getElementById('recommendations-author');
245
246 authorBooks.forEach(book => {
247 const bookCard = document.createElement('div');
248 bookCard.className = 'book-card';
249
250 bookCard.innerHTML = `
251 <img src="./BookImages/${book.image}" alt="${book.title}">
252 <a href='./BookView.php?bookid=${book.bookid}' style="text-decoration: none"><h3 class="book-title" style="color: green;">${book.title}</h3></a>
253 <p>${book.author}</p>
254 <p>${book.genre}</p>
255 `;
256
257 recommendationsAuthorContainer.appendChild(bookCard);
258 });
259 }
260
261 // Initialize the page
262 document.addEventListener('DOMContentLoaded', () => {
263 populateRecommendations();
264 populateMoreAuthor();
265
266 });
267 </script>
268
269<?php include 'Components/Footer.html'; ?>
270
271</body>
272</html>
Note: See TracBrowser for help on using the repository browser.