This commit is contained in:
Flatlogic Bot 2025-12-11 19:24:39 +00:00
parent 1d182fef38
commit dd86cddf44
3 changed files with 126 additions and 2 deletions

View File

@ -1,15 +1,34 @@
<?php <?php
session_start(); session_start();
require_once 'db/config.php'; // Include the database configuration
// If user is not logged in, redirect to login page // If user is not logged in, redirect to login page
if (!isset($_SESSION['user_id'])) { if (!isset($_SESSION['user_id'])) {
header("Location: login.php"); header("Location: login.php");
exit; exit;
} }
$user_id = $_SESSION['user_id'];
$user_name = $_SESSION['user_name']; $user_name = $_SESSION['user_name'];
$user_role = $_SESSION['user_role']; $user_role = $_SESSION['user_role'];
$pdo = db(); // Get the PDO connection
$items = []; // Initialize an empty array to hold items
if ($user_role === 'user') {
// Fetch items rented by the user
$stmt = $pdo->prepare("SELECT * FROM items WHERE renter_id = ? AND status = 'rented'");
$stmt->execute([$user_id]);
$items = $stmt->fetchAll();
} elseif ($user_role === 'vendor') {
// Fetch items listed by the vendor
$stmt = $pdo->prepare("SELECT * FROM items WHERE owner_id = ?");
$stmt->execute([$user_id]);
$items = $stmt->fetchAll();
}
?> ?>
<?php include 'header.php'; ?> <?php include 'header.php'; ?>
@ -24,7 +43,43 @@ $user_role = $_SESSION['user_role'];
<p>Welcome to your RentEase dashboard.</p> <p>Welcome to your RentEase dashboard.</p>
<p>Your role is: <strong><?php echo htmlspecialchars($user_role); ?></strong></p> <p>Your role is: <strong><?php echo htmlspecialchars($user_role); ?></strong></p>
<?php if ($user_role === 'vendor'): ?> <?php if ($user_role === 'vendor'): ?>
<a href="add_item.php" class="btn btn-primary">Add New Item</a> <a href="add_item.php" class="btn btn-primary mb-3">Add New Item</a>
<?php endif; ?>
<hr>
<?php if ($user_role === 'user'): ?>
<h5>Your Rented Items</h5>
<?php elseif ($user_role === 'vendor'): ?>
<h5>Your Listed Items</h5>
<?php endif; ?>
<?php if (empty($items)): ?>
<p>No items found.</p>
<?php else: ?>
<div class="row">
<?php foreach ($items as $item): ?>
<div class="col-md-4 mb-4">
<div class="card h-100">
<img src="<?php echo htmlspecialchars($item['image_url'] ?? 'https://via.placeholder.com/150'); ?>" class="card-img-top" alt="<?php echo htmlspecialchars($item['name']); ?>">
<div class="card-body d-flex flex-column">
<h5 class="card-title"><?php echo htmlspecialchars($item['name']); ?></h5>
<p class="card-text flex-grow-1"><?php echo htmlspecialchars($item['description']); ?></p>
<p class="card-text"><strong>Price:</strong> $<?php echo htmlspecialchars(number_format($item['price'], 2)); ?> per day</p>
<p class="card-text"><strong>Location:</strong> <?php echo htmlspecialchars($item['location']); ?></p>
<div class="mt-auto">
<a href="item_details.php?id=<?php echo htmlspecialchars($item['id']); ?>" class="btn btn-primary">View Details</a>
<?php if ($user_role === 'user' && $item['status'] === 'rented'): ?>
<p class="text-success mt-2">Rented until: <?php echo htmlspecialchars($item['rented_date'] ? date('Y-m-d', strtotime($item['rented_date'] . ' +' . $item['rental_duration'] . ' days')) : 'N/A'); ?></p>
<?php elseif ($user_role === 'vendor'): ?>
<p class="text-info mt-2">Status: <?php echo htmlspecialchars(ucfirst($item['status'])); ?></p>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?> <?php endif; ?>
<p><a href='logout.php'>Click here to log out.</a></p> <p><a href='logout.php'>Click here to log out.</a></p>
</div> </div>

View File

@ -24,6 +24,16 @@ if (!$item) {
} }
?> ?>
<?php
if (isset($_GET['message']) && isset($_GET['type'])) {
$message = htmlspecialchars($_GET['message']);
$type = htmlspecialchars($_GET['type']);
echo '<div class="alert alert-' . $type . ' alert-dismissible fade show" role="alert">';
echo $message;
echo '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
echo '</div>';
}
?>
<div class="container section"> <div class="container section">
<div class="row"> <div class="row">
<div class="col-md-8 offset-md-2"> <div class="col-md-8 offset-md-2">
@ -36,7 +46,10 @@ if (!$item) {
<hr> <hr>
<div class="d-flex justify-content-between align-items-center mt-3"> <div class="d-flex justify-content-between align-items-center mt-3">
<h3 class="price-text">$<?php echo htmlspecialchars(number_format($item['price_per_day'], 2)); ?> / day</h3> <h3 class="price-text">$<?php echo htmlspecialchars(number_format($item['price_per_day'], 2)); ?> / day</h3>
<button class="btn btn-primary btn-lg">Rent Now</button> <form action="rent_item.php" method="POST">
<input type="hidden" name="item_id" value="<?php echo htmlspecialchars($item['item_id']); ?>">
<button type="submit" class="btn btn-primary btn-lg">Rent Now</button>
</form>
</div> </div>
</div> </div>
</div> </div>

56
rent_item.php Normal file
View File

@ -0,0 +1,56 @@
<?php
session_start();
require_once 'db/config.php';
// Redirect if user is not logged in
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit();
}
$message = '';
$message_type = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['item_id'])) {
$item_id = filter_var($_POST['item_id'], FILTER_SANITIZE_NUMBER_INT);
$user_id = $_SESSION['user_id'];
try {
$pdo = db();
// Check if item is available
$stmt = $pdo->prepare("SELECT status FROM items WHERE item_id = ?");
$stmt->execute([$item_id]);
$item = $stmt->fetch();
if ($item && $item['status'] === 'available') {
// Update item status to rented
$stmt = $pdo->prepare("UPDATE items SET status = 'rented', renter_id = ?, rented_date = NOW() WHERE item_id = ?");
$stmt->execute([$user_id, $item_id]);
$message = 'Item rented successfully!';
$message_type = 'success';
} else {
$message = 'Item is not available for rent or does not exist.';
$message_type = 'danger';
}
} catch (PDOException $e) {
error_log("Database error during rental: " . $e->getMessage());
$message = 'An error occurred during rental. Please try again.';
$message_type = 'danger';
}
} else {
$message = 'Invalid item ID.';
$message_type = 'danger';
}
} else {
header('Location: listings.php'); // Redirect if not a POST request
exit();
}
// Redirect back to item details with message
// You might want to pass these messages via session or URL parameters
header('Location: item_details.php?id=' . $item_id . '&message=' . urlencode($message) . '&type=' . $message_type);
exit();
?>