1 | <?php
|
---|
2 |
|
---|
3 | session_start();
|
---|
4 | require 'connect.php';
|
---|
5 |
|
---|
6 | // Get member_id from session
|
---|
7 | $userId = $_SESSION['userid'] ?? null;
|
---|
8 | if (!$userId) {
|
---|
9 | header('Location: ./Sign&Log.php');
|
---|
10 | }
|
---|
11 |
|
---|
12 | function getCartItems($conn, $memberId) {
|
---|
13 | $query = "
|
---|
14 | SELECT
|
---|
15 | book.bookid,
|
---|
16 | book.title,
|
---|
17 | book.publishedyear,
|
---|
18 | book.coverimage,
|
---|
19 | book.description,
|
---|
20 | string_agg(CONCAT(author.firstname, ' ', author.lastname), ', ') as author_names
|
---|
21 | FROM cart
|
---|
22 | JOIN book ON book.bookid = cart.bookid
|
---|
23 | JOIN book_author ON book.bookid = book_author.bookid
|
---|
24 | JOIN author ON book_author.authorid = author.authorid
|
---|
25 | WHERE cart.memberid = :memberid
|
---|
26 | GROUP BY book.bookid, book.title, book.publishedyear, book.coverimage, book.description;
|
---|
27 | ";
|
---|
28 |
|
---|
29 | $stmt = $conn->prepare($query);
|
---|
30 | $stmt->execute(['memberid' => $memberId]);
|
---|
31 | return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
---|
32 | }
|
---|
33 |
|
---|
34 | // Assuming you store user_id in session and need to get member_id
|
---|
35 | $userId = $_SESSION['userid'] ?? null;
|
---|
36 | if ($userId) {
|
---|
37 | $memberQuery = "SELECT memberid FROM member WHERE userid = :userid";
|
---|
38 | $stmt = $conn->prepare($memberQuery);
|
---|
39 | $stmt->execute(['userid' => $userId]);
|
---|
40 | $member = $stmt->fetch(PDO::FETCH_ASSOC);
|
---|
41 | $memberId = $member['memberid'] ?? null;
|
---|
42 | } else {
|
---|
43 | // Redirect to login if no user is logged in
|
---|
44 | header('Location: ./Sign&Log.php');
|
---|
45 | exit();
|
---|
46 | }
|
---|
47 |
|
---|
48 | // Fetch cart items
|
---|
49 | $cartItems = $memberId ? getCartItems($conn, $memberId) : [];
|
---|
50 |
|
---|
51 | if (isset($_GET['submit']) == 'remove-from-cart') {
|
---|
52 | try {
|
---|
53 | // Delete from cart
|
---|
54 | $deleteQuery = "DELETE FROM cart WHERE memberid = :memberid AND bookid = :bookid";
|
---|
55 | $deleteStmt = $conn->prepare($deleteQuery);
|
---|
56 | $deleteStmt->execute([
|
---|
57 | 'memberid' => $member['memberid'],
|
---|
58 | 'bookid' => $_GET['bookid']
|
---|
59 | ]);
|
---|
60 |
|
---|
61 | header("Location: ./Cart.php");
|
---|
62 | exit();
|
---|
63 |
|
---|
64 | } catch (PDOException $e) {
|
---|
65 | error_log("Database error: " . $e->getMessage());
|
---|
66 | echo "An error occurred while removing the book from your cart.";
|
---|
67 | exit();
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | ?>
|
---|
72 |
|
---|
73 | <!DOCTYPE html>
|
---|
74 | <html lang="en">
|
---|
75 | <head>
|
---|
76 | <meta charset="UTF-8">
|
---|
77 | <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
---|
78 | <title>Shopping Cart</title>
|
---|
79 | <link rel="stylesheet" href="CSS/Cart.css">
|
---|
80 | </head>
|
---|
81 | <body>
|
---|
82 |
|
---|
83 | <?php include 'Components/Header.html'; ?>
|
---|
84 |
|
---|
85 |
|
---|
86 | <div class="container">
|
---|
87 | <div class="breadcrumb">
|
---|
88 | <a href="#">Books</a> / <a href="#">Cart</a>
|
---|
89 | </div>
|
---|
90 |
|
---|
91 | <div class="cart-page">
|
---|
92 | <h1>Your Cart</h1>
|
---|
93 |
|
---|
94 | <div class="cart-items">
|
---|
95 | <?php if (empty($cartItems)): ?>
|
---|
96 | <p>Your cart is empty.</p>
|
---|
97 | <?php else: ?>
|
---|
98 | <?php foreach ($cartItems as $item): ?>
|
---|
99 | <div class="cart-item">
|
---|
100 | <div class="cart-item-image">
|
---|
101 | <?php if ($item['coverimage']): ?>
|
---|
102 | <img src="BookImages/<?php echo htmlspecialchars($item['coverimage']); ?>" alt="Book Cover">
|
---|
103 | <?php else: ?>
|
---|
104 | <img src="images/placeholder-book.png" alt="Book Cover">
|
---|
105 | <?php endif; ?>
|
---|
106 | </div>
|
---|
107 | <div class="cart-item-info">
|
---|
108 | <h2><?php echo htmlspecialchars($item['title']); ?></h2>
|
---|
109 | <p>By <?php echo htmlspecialchars($item['author_names']); ?></p>
|
---|
110 | <p>Publication Year: <?php echo htmlspecialchars($item['publishedyear']); ?></p>
|
---|
111 | </div>
|
---|
112 | <div class="cart-item-actions">
|
---|
113 | <form method="GET" action="Cart.php">
|
---|
114 | <input type="hidden" name="bookid" value="<?php echo $item['bookid']; ?>">
|
---|
115 | <button type="submit" name="submit" value="remove-from-cart" class="btn btn-secondary">Remove</button>
|
---|
116 | </form>
|
---|
117 | </div>
|
---|
118 | </div>
|
---|
119 | <?php endforeach; ?>
|
---|
120 | <?php endif; ?>
|
---|
121 | </div>
|
---|
122 |
|
---|
123 | <div class="cart-summary">
|
---|
124 | <h2>Summary</h2>
|
---|
125 | <div class="summary-item">
|
---|
126 | <span>Total Books: <?php echo count($cartItems); ?> </span>
|
---|
127 | </div>
|
---|
128 | <form action="./BorrowBook.php" method="POST">
|
---|
129 | <input type="hidden" name="memberid" value="<?php echo $_SESSION['userid'] ?>">
|
---|
130 | <button class="btn btn-primary" <?php if (count($cartItems) == 0) { echo 'disabled'; } ?>>Borrow</button>
|
---|
131 | </form>
|
---|
132 | </div>
|
---|
133 | </div>
|
---|
134 | </div>
|
---|
135 |
|
---|
136 | <?php include 'Components/Footer.html'; ?>
|
---|
137 | </body>
|
---|
138 | </html> |
---|