134 lines
6.4 KiB
PHP
134 lines
6.4 KiB
PHP
<?php
|
|
session_start();
|
|
|
|
// If the user is not logged in, redirect to the login page...
|
|
if (!isset($_SESSION['loggedin'])) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
$page_title = "Dashboard";
|
|
$page_description = "Main dashboard for the Student Management System.";
|
|
|
|
// Fetch a few recent students to display
|
|
$students = [];
|
|
try {
|
|
require_once __DIR__ . '/db/config.php';
|
|
$pdo = db();
|
|
// Ensure table exists before querying
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS students (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
first_name VARCHAR(100) NOT NULL,
|
|
last_name VARCHAR(100) NOT NULL,
|
|
date_of_birth DATE NOT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
);");
|
|
$stmt = $pdo->query("SELECT id, first_name, last_name, created_at FROM students ORDER BY created_at DESC LIMIT 5");
|
|
if($stmt) {
|
|
$students = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
} catch (Exception $e) {
|
|
error_log("DB Error on dashboard: " . $e->getMessage());
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title><?= htmlspecialchars($page_title) ?> - <?= htmlspecialchars($_SERVER['PROJECT_NAME'] ?? 'WebApp') ?></title>
|
|
<meta name="description" content="<?= htmlspecialchars($page_description) ?>">
|
|
<meta property="og:title" content="<?= htmlspecialchars($_SERVER['PROJECT_NAME']) ?>">
|
|
<meta property="og:description" content="<?= htmlspecialchars($_SERVER['PROJECT_DESCRIPTION']) ?>">
|
|
<meta property="og:image" content="<?= htmlspecialchars($_SERVER['PROJECT_IMAGE_URL']) ?>">
|
|
<meta property="twitter:card" content="summary_large_image">
|
|
<meta property="twitter:title" content="<?= htmlspecialchars($_SERVER['PROJECT_NAME']) ?>">
|
|
<meta property="twitter:description" content="<?= htmlspecialchars($_SERVER['PROJECT_DESCRIPTION']) ?>">
|
|
<meta property="twitter:image" content="<?= htmlspecialchars($_SERVER['PROJECT_IMAGE_URL']) ?>">
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
|
|
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700&display=swap" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css">
|
|
</head>
|
|
<body class="bg-light">
|
|
|
|
<nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
|
|
<div class="container-fluid">
|
|
<a class="navbar-brand" href="index.php"><?= htmlspecialchars($_SERVER['PROJECT_NAME'] ?? 'WebApp') ?></a>
|
|
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
|
|
<span class="navbar-toggler-icon"></span>
|
|
</button>
|
|
<div class="collapse navbar-collapse" id="navbarNav">
|
|
<ul class="navbar-nav ms-auto">
|
|
<li class="nav-item">
|
|
<a class="nav-link" href="#">Welcome, <?= htmlspecialchars($_SESSION['username']) ?>!</a>
|
|
</li>
|
|
<li class="nav-item">
|
|
<a class="nav-link" href="logout.php">Logout</a>
|
|
</li>
|
|
<?php if ($_SESSION['role'] === 'admin'): ?>
|
|
<li class="nav-item">
|
|
<a class="nav-link" href="admin.php">Admin</a>
|
|
</li>
|
|
<?php endif; ?>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
|
|
<main class="container my-5">
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<h1 class="h2">Dashboard</h1>
|
|
<a href="add_student.php" class="btn btn-primary">
|
|
<i class="bi bi-plus-circle-fill me-2"></i>Add New Student
|
|
</a>
|
|
</div>
|
|
|
|
<div class="card border-0 shadow-sm">
|
|
<div class="card-header bg-white border-bottom-0">
|
|
<h5 class="card-title mb-0">Recently Added Students</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if (empty($students)): ?>
|
|
<p class="text-center text-muted">No students have been added yet. <a href="add_student.php">Add the first one!</a></p>
|
|
<?php else: ?>
|
|
<div class="table-responsive">
|
|
<table class="table table-hover align-middle">
|
|
<thead class="table-light">
|
|
<tr>
|
|
<th scope="col">#</th>
|
|
<th scope="col">First Name</th>
|
|
<th scope="col">Last Name</th>
|
|
<th scope="col">Date Added</th>
|
|
<th scope="col" class="text-end">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($students as $student): ?>
|
|
<tr>
|
|
<th scope="row"><?= htmlspecialchars($student['id']) ?></th>
|
|
<td><?= htmlspecialchars($student['first_name']) ?></td>
|
|
<td><?= htmlspecialchars($student['last_name']) ?></td>
|
|
<td><?= date("M d, Y", strtotime($student['created_at'])) ?></td>
|
|
<td class="text-end">
|
|
<a href="student.php?id=<?= htmlspecialchars($student['id']) ?>" class="btn btn-sm btn-outline-primary">View</a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<footer class="text-center py-4 text-muted">
|
|
<p>© <?= date('Y') ?> <?= htmlspecialchars($_SERVER['PROJECT_NAME'] ?? 'WebApp') ?>. All Rights Reserved.</p>
|
|
</footer>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script src="assets/js/main.js"></script>
|
|
</body>
|
|
</html>
|