-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdescription.php
More file actions
150 lines (132 loc) · 5.61 KB
/
description.php
File metadata and controls
150 lines (132 loc) · 5.61 KB
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
<?php
session_start();
// Include database config
include "db.php";
// Get book ID from URL
$book_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
if ($book_id <= 0) {
echo "Invalid book ID.";
exit;
}
// Fetch book data
$query = "SELECT * FROM books WHERE id = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $book_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows == 0) {
echo "Book not found.";
exit;
}
$book = $result->fetch_assoc();
// Check if user can review
$can_review = false;
if (isset($_SESSION['user_id'])) {
$user_id = $_SESSION['user_id'];
$query = "SELECT COUNT(*) as completed_orders
FROM orders o
JOIN order_items oi ON o.id = oi.order_id
WHERE o.user_id = ? AND oi.book_id = ? AND o.status = 'completed'";
$stmt = $conn->prepare($query);
$stmt->bind_param("ii", $user_id, $book_id);
$stmt->execute();
$result = $stmt->get_result();
$completed_orders = $result->fetch_assoc()['completed_orders'] ?? 0;
$query = "SELECT COUNT(*) as user_reviews FROM reviews WHERE user_id = ? AND book_id = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("ii", $user_id, $book_id);
$stmt->execute();
$result = $stmt->get_result();
$user_reviews = $result->fetch_assoc()['user_reviews'] ?? 0;
if ($completed_orders > $user_reviews) {
$can_review = true;
}
}
include "header.php";
?>
<!-- -->
<div class="container mt-5">
<h2 class="text-center mb-4"><?= htmlspecialchars($book["title"]); ?></h2>
<div class="row">
<div class="col-md-6">
<img src="<?= htmlspecialchars($book['image'] ?? 'Lore.jpg'); ?>" class="card-img-top" alt="<?= htmlspecialchars($book['title']); ?>">
</div>
<div class="col-md-6">
<p><strong>Author:</strong> <?= htmlspecialchars($book["author"]); ?></p>
<p><strong>Price:</strong> $<?= htmlspecialchars($book["price"]); ?></p>
<p><strong>Description:</strong> <?= nl2br(htmlspecialchars($book["description"])); ?></p>
<?php if (isset($_SESSION["user_id"])): ?>
<a href="cart.php?id=<?= $book["id"]; ?>" class="btn btn-primary">Add to Cart</a>
<?php else: ?>
<button class="btn btn-warning" onclick="redirectToLogin();">Add to Cart</button>
<?php endif; ?>
</div>
</div>
<div class="mt-5">
<h4><i class="fas fa-comments"></i> Reviews</h4>
<div class="row">
<?php
$query = "SELECT r.rating, r.review, u.name AS username FROM reviews r
JOIN users u ON r.user_id = u.id
WHERE r.book_id = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $book_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
while ($review = $result->fetch_assoc()) {
echo "<div class='col-md-6'>";
echo "<div class='card review-card shadow-sm mb-3'>";
echo "<div class='card-body'>";
echo "<div class='d-flex align-items-center'>";
echo "<i class='fas fa-user-circle profile-icon'></i>";
echo "<h5 class='card-title'>" . htmlspecialchars($review["username"]) . "</h5>";
echo "</div>";
echo "<h6 class='card-subtitle mb-2 text-muted'>Rating: " . str_repeat('⭐', $review["rating"]) . "</h6>";
echo "<p class='card-text'><strong>Comment:</strong> " . nl2br(htmlspecialchars($review["review"])) . "</p>";
echo "</div></div></div>";
}
} else {
echo "<p class='col-12'>No reviews yet.</p>";
}
?>
</div>
</div>
<?php if ($can_review): ?>
<div class="mt-5">
<h4><i class="fas fa-pen"></i> Leave a Review</h4>
<div class="card shadow">
<div class="card-body">
<form action="submit_review.php" method="POST">
<input type="hidden" name="book_id" value="<?= htmlspecialchars($book_id); ?>">
<!-- star -->
<div class="form-group">
<label for="rating">Rating (1 to 5 stars):</label>
<input type="number" id="rating" name="rating" min="1" max="5" class="form-control" required>
</div>
<!-- review -->
<div class="form-group">
<label for="review">Review:</label>
<textarea id="review" name="review" class="form-control" rows="5" required></textarea>
</div>
<button type="submit" class="btn btn-success">Submit Review</button>
</form>
</div>
</div>
</div>
<?php else: ?>
<p class="mt-3 text-muted">You must complete the book delivery before submitting a new review or you’ve already reviewed all your purchases.</p>
<?php endif; ?>
</div>
<script>
function redirectToLogin() {
alert("You are not logged in. Please log in first.");
window.location.href = "login.php";
}
</script>
<!-- Bootstrap & JS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://kit.fontawesome.com/a076d05399.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.bundle.min.js"></script>
<?php include "footer.php"; ?>