You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
FutureCraft/donation_list.php

78 lines
2.6 KiB

6 months ago
<?php
require 'db_connection.php';
session_start();
// Ensure the user is logged in
if (!isset($_SESSION['user_email'])) {
header("Location: login.php");
exit;
}
// Get the logged-in user's ID and role
$user_id = $_SESSION['user_id'];
$role = $_SESSION['role'];
// Query the database based on the user's role
if ($role === 'donor') {
// Fetch donations made by the logged-in donor
$stmt = $conn->prepare("SELECT * FROM material_donations WHERE user_id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$donations = $result->fetch_all(MYSQLI_ASSOC);
} elseif ($role === 'recipient') {
// Fetch donations available for the recipient (donations by other users)
$stmt = $conn->prepare("SELECT * FROM material_donations WHERE user_id != ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$donations = $result->fetch_all(MYSQLI_ASSOC);
} else {
// If the user is neither donor nor recipient (should not happen)
echo "Invalid role.";
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Donation List</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
</head>
<body>
<?php include 'includes/header.php'; ?>
<div class="container mt-5">
<h2><?php echo $role === 'donor' ? 'Your Donation List' : 'Available Donations for You'; ?></h2>
<?php if (empty($donations)): ?>
<div class="alert alert-info">No donations available.</div>
<?php else: ?>
<table class="table table-bordered">
<thead>
<tr>
<th>Item Name</th>
<th>Condition</th>
<th>Quantity</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<?php foreach ($donations as $donation): ?>
<tr>
<td><?php echo htmlspecialchars($donation['item_name']); ?></td>
<td><?php echo htmlspecialchars($donation['condition']); ?></td>
<td><?php echo htmlspecialchars($donation['quantity']); ?></td>
<td><?php echo htmlspecialchars($donation['notes']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</body>
</html>