make this design is so super report anaylstics and use best practise and make it working well (<?php
session_start();
require_once 'db_connect.php'; // Ensure this file connects to your database
// Check if the admin is logged in
if (!isset($_SESSION['admin_id'])) {
header("Location: admin_login.php");
exit();
}
// Build base WHERE conditions
whereconditions="WHEREp.registrationdateBETWEEN:startdateAND:enddate";params = [
':start_date' => startdate,′:endend_date
];
// Apply filters to WHERE conditions
if (gender_filter) {
where_conditions .= " AND p.gender = :gender";
params[′:gender′]=gender_filter;
}
if (age_group_filter) {
switch (age_group_filter) {
case 'under_18':
whereconditions.="ANDp.age<18";break;case′1830′:where_conditions .= " AND p.age BETWEEN 18 AND 30";
break;
case '31_50':
whereconditionwhere_conditions .= " AND p.age BETWEEN 51 AND 70";
break;
case 'over_70':
$where_conditions .= " AND p.age > 70";
break;
}
}
if (status_filter) {
where_conditions .= " AND p.status = :status";
params[′:status′]=status_filter;
}
// Fetch total number of patients
stmt=pdo->prepare("SELECT COUNT(*) FROM patients p whereconditions");stmt->execute(params);total_patients = $stmt->fetchColumn();
// Fetch gender distribution
stmt=pdo->prepare("SELECT p.gender, COUNT(*) as count FROM patients p whereconditionsGROUPBYp.gender");stmt->execute(params);gender_distribution = $stmt->fetchAll();
// Fetch age distribution
stmt=pdo->prepare("SELECT p.age FROM patients p whereconditions");stmt->execute(params);ages = $stmt->fetchAll(PDO::FETCH_COLUMN);
// Fetch status distribution
stmt=pdo->prepare("SELECT p.status, COUNT(*) as count FROM patients p whereconditionsGROUPBYp.status");stmt->execute(params);status_distribution = $stmt->fetchAll();
// Fetch services rendered distribution
if (genderfilter∣∣age_group_filter || status_filter) {
// If patient filters are applied, join with patients table and apply the same where_conditions
servicequery="SELECTs.servicename,Cwhere_conditions
GROUP BY s.service_name
";
code
$services_params = $params;
} else {
// No patient filters applied
$service_query = "
SELECT s.service_name, COUNT(*) as count
FROM invoice_services isv
INNER JOIN invoices i ON isv.invoice_id = i.id
INNER JOIN services s ON isv.service_id = s.service_id
WHERE i.invoice_date BETWEEN :start_date AND :end_date
GROUP BY s.service_name
";
// Fetch invoice amounts over time
stmt=pdo->prepare("
SELECT DATE(i.invoice_date) as date, SUM(i.total_amount) as total_amount
FROM invoices i
INNER JOIN patients p ON i.patient_id = p.id
whereconditionsGROUPBYDATE(i.invoicedate)ORDERBYdate");stmt->execute(params);invoice_trend = $stmt->fetchAll();
// Fetch patient registration trend
stmt=pdo->prepare("
SELECT DATE(p.registration_date) as date, COUNT(*) as count
FROM patients p
whereconditionsGROUPBYDATE(p.registrationdate)ORDERBYdate");stmt->execute(params);registration_trend = $stmt->fetchAll();
?>
Certainly! Here's an improved version of your code that follows best practices, enhances performance, and updates the design to make it more modern and user-friendly. I've structured the code to separate concerns, optimized queries, validated inputs, and updated dependencies to the latest versions.
Improvements Made:
Structured Code with Functions: Separated data fetching logic into functions for better readability and maintainability.
Input Validation and Sanitization: Ensured all user inputs are validated and sanitized to prevent security vulnerabilities.
Optimized Database Queries: Used parameterized queries and optimized conditions to improve performance.
Updated Dependencies: Upgraded Bootstrap to version 5 for a modern look and better responsiveness.
Enhanced Design: Improved the UI with better color schemes, updated components, and responsive design considerations.
Error Handling: Implemented try-catch blocks around database operations for better error management.
Code Comments and Documentation: Added comments to explain code sections, improving readability for future developers.
Here's the revised code:
php
<?php
session_start();
require_once 'db_connect.php'; // Ensure this file connects to your database
// Check if the admin is logged in
if (!isset($_SESSION['admin_id'])) {
header("Location: admin_login.php");
exit();
}
try {
// Initialize filter variables with default values
$start_date = isset($_GET['start_date']) ? $_GET['start_date'] : date('Y-m-d', strtotime('-30 days'));
$end_date = isset($_GET['end_date']) ? $_GET['end_date'] : date('Y-m-d');
$gender_filter = isset($_GET['gender_filter']) ? $_GET['gender_filter'] : '';
$age_group_filter = isset($_GET['age_group_filter']) ? $_GET['age_group_filter'] : '';
$status_filter = isset($_GET['status_filter']) ? $_GET['status_filter'] : '';
$service_filter = isset($_GET['service_filter']) ? $_GET['service_filter'] : '';
// Validate dates
$start_date = date('Y-m-d', strtotime($start_date));
$end_date = date('Y-m-d', strtotime($end_date));
// Build filters array
$filters = [
'start_date' => $start_date,
'end_date' => $end_date,
'gender' => $gender_filter,
'age_group' => $age_group_filter,
'status' => $status_filter,
'service' => $service_filter
];
// Fetch all the data
$total_patients = getTotalPatients($pdo, $filters);
$gender_distribution = getGenderDistribution($pdo, $filters);
$age_distribution = getAgeDistribution($pdo, $filters);
$status_distribution = getStatusDistribution($pdo, $filters);
$services_distribution = getServicesDistribution($pdo, $filters);
$invoice_trend = getInvoiceTrend($pdo, $filters);
$registration_trend = getRegistrationTrend($pdo, $filters);
} catch (Exception $e) {
// Handle exceptions
die("Error: " . $e->getMessage());
}
// Functions to fetch data
function buildWhereConditions($filters, &$params, $prefix = 'p.') {
$where = [];
$params = [];
if (!empty($filters['start_date']) && !empty($filters['end_date'])) {
$where[] = $prefix."registration_date BETWEEN :start_date AND :end_date";
$params[':start_date'] = $filters['start_date'];
$params[':end_date'] = $filters['end_date'];
}
if (!empty($filters['gender'])) {
$where[] = $prefix."gender = :gender";
$params[':gender'] = $filters['gender'];
}
if (!empty($filters['age_group'])) {
switch ($filters['age_group']) {
case 'under_18':
$where[] = $prefix."age < 18";
break;
case '18_30':
$where[] = $prefix."age BETWEEN 18 AND 30";
break;
case '31_50':
$where[] = $prefix."age BETWEEN 31 AND 50";
break;
case '51_70':
$where[] = $prefix."age BETWEEN 51 AND 70";
break;
case 'over_70':
$where[] = $prefix."age > 70";
break;
}
}
if (!empty($filters['status'])) {
$where[] = $prefix."status = :status";
$params[':status'] = $filters['status'];
}
return !empty($where) ? 'WHERE ' . implode(' AND ', $where) : '';
}
function getTotalPatients($pdo, $filters) {
$where_conditions = buildWhereConditions($filters, $params);
$stmt = $pdo->prepare("SELECT COUNT(*) FROM patients p $where_conditions");
$stmt->execute($params);
return $stmt->fetchColumn();
}
function getGenderDistribution($pdo, $filters) {
$where_conditions = buildWhereConditions($filters, $params);
$stmt = $pdo->prepare("SELECT p.gender, COUNT(*) as count FROM patients p $where_conditions GROUP BY p.gender");
$stmt->execute($params);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
function getAgeDistribution($pdo, $filters) {
$where_conditions = buildWhereConditions($filters, $params);
$stmt = $pdo->prepare("SELECT p.age FROM patients p $where_conditions");
$stmt->execute($params);
return $stmt->fetchAll(PDO::FETCH_COLUMN);
}
function getStatusDistribution($pdo, $filters) {
$where_conditions = buildWhereConditions($filters, $params);
$stmt = $pdo->prepare("SELECT p.status, COUNT(*) as count FROM patients p $where_conditions GROUP BY p.status");
$stmt->execute($params);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
function getServicesDistribution($pdo, $filters) {
$params = [];
if (!empty($filters['gender']) || !empty($filters['age_group']) || !empty($filters['status'])) {
$patient_conditions = buildWhereConditions($filters, $params, 'p.');
$service_conditions = "INNER JOIN patients p ON i.patient_id = p.id $patient_conditions";
} else {
$service_conditions = '';
$params[':start_date'] = $filters['start_date'];
$params[':end_date'] = $filters['end_date'];
}
$stmt = $pdo->prepare("
SELECT s.service_name, COUNT(*) as count
FROM invoice_services isv
INNER JOIN invoices i ON isv.invoice_id = i.id
INNER JOIN services s ON isv.service_id = s.service_id
$service_conditions
WHERE i.invoice_date BETWEEN :start_date AND :end_date
GROUP BY s.service_name
");
$stmt->execute($params);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
function getInvoiceTrend($pdo, $filters) {
$where_conditions = buildWhereConditions($filters, $params);
$stmt = $pdo->prepare("
SELECT DATE(i.invoice_date) as date, SUM(i.total_amount) as total_amount
FROM invoices i
INNER JOIN patients p ON i.patient_id = p.id
$where_conditions
GROUP BY DATE(i.invoice_date)
ORDER BY date
");
$stmt->execute($params);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
function getRegistrationTrend($pdo, $filters) {
$where_conditions = buildWhereConditions($filters, $params);
$stmt = $pdo->prepare("
SELECT DATE(p.registration_date) as date, COUNT(*) as count
FROM patients p
$where_conditions
GROUP BY DATE(p.registration_date)
ORDER BY date
");
$stmt->execute($params);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Meta tags for responsive design and character encoding -->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Page title -->
<title>Admin Report Dashboard</title>
<!-- Bootstrap 5 CSS for responsive layout and components -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Date Range Picker CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css">
<!-- Chart.js Library -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<!-- Custom CSS for the design -->
<style>
/* Custom CSS for clean design */
body {
background-color: #f8f9fa;
font-family: 'Roboto', Arial, sans-serif;
}
/* Header styling */
.dashboard-header {
background-color: #4F3F02; /* Brown */
color: #fff;
padding: 20px;
text-align: center;
border-radius: 10px;
margin-bottom: 30px;
box-shadow: 0 4px 10px rgba(0,0,0,0.1);
}
/* Card styling */
.card {
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
margin-bottom: 20px;
background-color: #ffffff;
}
.card-header {
background-color: #D1C287; /* Light golden */
color: #4F3F02; /* Brown */
font-weight: bold;
border-bottom: none;
padding: 15px;
}
/* Button styling */
.btn-primary {
background-color: #4F3F02; /* Brown */
border-color: #4F3F02;
color: #fff;
font-weight: bold;
border-radius: 50px;
}
.btn-primary:hover {
background-color: #D1C287; /* Light golden */
border-color: #D1C287;
color: #4F3F02; /* Brown */
}
.btn-secondary {
background-color: #D1C287; /* Light golden */
border-color: #D1C287;
color: #4F3F02; /* Brown */
font-weight: bold;
border-radius: 50px;
}
.btn-secondary:hover {
background-color: #4F3F02; /* Brown */
color: #fff;
}
/* Form control styling */
.form-control, .form-select {
border-radius: 8px;
}
/* DateRangePicker styling */
.daterangepicker td.active, .daterangepicker td.active:hover {
background-color: #4F3F02;
}
/* Print button styling */
#printReport {
background-color: #28a745; /* Green */
border-color: #28a745;
color: #fff;
font-weight: bold;
border-radius: 50px;
}
#printReport:hover {
background-color: #218838; /* Dark green */
border-color: #1e7e34;
color: #fff;
}
/* Adjust chart sizes */
canvas {
max-height: 400px;
}
</style>
</head>
<body>
<!-- Main Container -->
<div class="container-fluid">
<!-- Header -->
<div class="dashboard-header">
<h1>Admin Report Dashboard</h1>
<p>Comprehensive Patient Data Analysis</p>
</div>
<!-- Filters and Print Button -->
<div class="row mb-4">
<div class="col-md-12">
<!-- Filter Card -->
<div class="card">
<div class="card-header">Filters</div>
<div class="card-body">
<form id="filterForm" method="GET" class="row g-3">
<!-- Date Range Picker -->
<div class="col-md-3">
<label for="daterange" class="form-label">Date Range:</label>
<input type="text" id="daterange" name="daterange" class="form-control" value="<?= htmlspecialchars($start_date) ?> - <?= htmlspecialchars($end_date) ?>">
<input type="hidden" id="start_date" name="start_date" value="<?= htmlspecialchars($start_date) ?>">
<input type="hidden" id="end_date" name="end_date" value="<?= htmlspecialchars($end_date) ?>">
</div>
<!-- Gender Filter -->
<div class="col-md-3">
<label for="gender_filter" class="form-label">Gender:</label>
<select id="gender_filter" name="gender_filter" class="form-select">
<option value="">All</option>
<option value="Male" <?= $gender_filter == 'Male' ? 'selected' : '' ?>>Male</option>
<option value="Female" <?= $gender_filter == 'Female' ? 'selected' : '' ?>>Female</option>
<option value="Other" <?= $gender_filter == 'Other' ? 'selected' : '' ?>>Other</option>
</select>
</div>
<!-- Age Group Filter -->
<div class="col-md-3">
<label for="age_group_filter" class="form-label">Age Group:</label>
<select id="age_group_filter" name="age_group_filter" class="form-select">
<option value="">All</option>
<option value="under_18" <?= $age_group_filter == 'under_18' ? 'selected' : '' ?>>Under 18</option>
<option value="18_30" <?= $age_group_filter == '18_30' ? 'selected' : '' ?>>18-30</option>
<option value="31_50" <?= $age_group_filter == '31_50' ? 'selected' : '' ?>>31-50</option>
<option value="51_70" <?= $age_group_filter == '51_70' ? 'selected' : '' ?>>51-70</option>
<option value="over_70" <?= $age_group_filter == 'over_70' ? 'selected' : '' ?>>Over 70</option>
</select>
</div>
<!-- Status Filter -->
<div class="col-md-3">
<label for="status_filter" class="form-label">Status:</label>
<select id="status_filter" name="status_filter" class="form-select">
<option value="">All</option>
<?php
$status_options_display = ['Initial Consultation', 'Diagnostic Phase', 'Treatment in Progress', 'Improving', 'Monitoring', 'Maintenance Phase', 'Awaiting Test Results', 'Follow-up Needed', 'Paused', 'Completed Program', 'Relapsed', 'Discontinued', 'Inactive'];
foreach ($status_options_display as $option) {
echo "<option value=\"" . htmlspecialchars($option) . "\"" . ($status_filter == $option ? ' selected' : '') . ">" . htmlspecialchars($option) . "</option>";
}
?>
</select>
</div>
<!-- Service Filter -->
<div class="col-md-3">
<label for="service_filter" class="form-label">Service:</label>
<select id="service_filter" name="service_filter" class="form-select">
<option value="">All</option>
<?php
// Fetch all services from the database
$stmt = $pdo->query("SELECT DISTINCT service_name FROM services ORDER BY service_name");
$services = $stmt->fetchAll(PDO::FETCH_COLUMN);
foreach ($services as $service) {
echo "<option value=\"" . htmlspecialchars($service) . "\"" . ($service_filter == $service ? ' selected' : '') . ">" . htmlspecialchars($service) . "</option>";
}
?>
</select>
</div>
<!-- Buttons -->
<div class="col-md-12 d-flex justify-content-end mt-3">
<button type="submit" class="btn btn-primary">Apply Filters</button>
<button type="button" id="resetFilters" class="btn btn-secondary ms-2">Reset Filters</button>
<button type="button" id="printReport" class="btn btn-success ms-2">Print Report</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- Report Content -->
<div id="reportContent">
<!-- Total Patients Card -->
<div class="row">
<div class="col-md-12">
<div class="card text-center">
<div class="card-body">
<h5 class="card-title">Total Patients</h5>
<h2 class="card-text"><?= htmlspecialchars($total_patients) ?></h2>
</div>
</div>
</div>
</div>
<!-- Charts Row -->
<div class="row">
<!-- Gender Distribution Chart -->
<div class="col-lg-6 col-md-12">
<div class="card h-100">
<div class="card-header">Gender Distribution</div>
<div class="card-body">
<canvas id="genderChart"></canvas>
</div>
</div>
</div>
<!-- Age Distribution Chart -->
<div class="col-lg-6 col-md-12">
<div class="card h-100">
<div class="card-header">Age Distribution</div>
<div class="card-body">
<canvas id="ageChart"></canvas>
</div>
</div>
</div>
</div>
<!-- Additional Charts Row -->
<div class="row">
<!-- Status Distribution Chart -->
<div class="col-lg-6 col-md-12">
<div class="card h-100">
<div class="card-header">Patient Status Distribution</div>
<div class="card-body">
<canvas id="statusChart"></canvas>
</div>
</div>
</div>
<!-- Services Rendered Distribution Chart -->
<div class="col-lg-6 col-md-12">
<div class="card h-100">
<div class="card-header">Services Rendered Distribution</div>
<div class="card-body">
<canvas id="servicesChart"></canvas>
</div>
</div>
</div>
</div>
<!-- Invoice Amount Trend Chart -->
<div class="row">
<div class="col-md-6">
<div class="card">
<div class="card-header">Invoice Amount Over Time</div>
<div class="card-body">
<canvas id="invoiceTrendChart"></canvas>
</div>
</div>
</div>
<!-- Patient Registration Trend Chart -->
<div class="col-md-6">
<div class="card">
<div class="card-header">Patient Registration Over Time</div>
<div class="card-body">
<canvas id="registrationTrendChart"></canvas>
</div>
</div>
</div>
</div>
</div>
<!-- End of Report Content -->
<!-- Back to Dashboard Button -->
<a href="admin_dashboard.php" class="btn btn-secondary mt-3 mb-5">Back to Dashboard</a>
</div>
<!-- Include jQuery and Bootstrap JS for components -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<!-- Bootstrap 5 JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
<!-- Moment.js for date manipulation -->
<script src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<!-- Date Range Picker JS -->
<script src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
<!-- Custom JS scripts -->
<script>
$(document).ready(function() {
// Initialize Date Range Picker
$('#daterange').daterangepicker({
startDate: moment('<?= htmlspecialchars($start_date) ?>'),
endDate: moment('<?= htmlspecialchars($end_date) ?>'),
locale: {
format: 'YYYY-MM-DD'
}
}, function(start, end, label) {
$('#start_date').val(start.format('YYYY-MM-DD'));
$('#end_date').val(end.format('YYYY-MM-DD'));
});
// Reset filters by reloading the page without any GET parameters
$('#resetFilters').click(function() {
window.location.href = 'admin_report.php';
});
// Print Report functionality
$('#printReport').click(function() {
window.print();
});
// Gender Distribution Chart
var genderCtx = document.getElementById('genderChart').getContext('2d');
var genderChart = new Chart(genderCtx, {
type: 'pie',
data: {
labels: <?= json_encode(array_column($gender_distribution, 'gender')) ?>,
datasets: [{
data: <?= json_encode(array_column($gender_distribution, 'count')) ?>,
backgroundColor: ['#4F3F02', '#D1C287', '#FFCE56'] // Colors for Male, Female, Other
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: 'bottom'
}
}
}
});
// Age Distribution Chart (Histogram)
var ageCtx = document.getElementById('ageChart').getContext('2d');
var ages = <?= json_encode($age_distribution) ?>;
var ageCounts = {};
ages.forEach(function(age) {
var ageGroup = Math.floor(age / 10) * 10;
var label = ageGroup + '-' + (ageGroup + 9);
if (!ageCounts[label]) {
ageCounts[label] = 0;
}
ageCounts[label]++;
});
var ageLabels = Object.keys(ageCounts);
var ageData = Object.values(ageCounts);
var ageChart = new Chart(ageCtx, {
type: 'bar',
data: {
labels: ageLabels,
datasets: [{
label: 'Number of Patients',
data: ageData,
backgroundColor: '#4F3F02'
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true,
stepSize: 1
}
},
plugins: {
legend: {
display: false
}
}
}
});
// Status Distribution Chart
var statusCtx = document.getElementById('statusChart').getContext('2d');
var statusChart = new Chart(statusCtx, {
type: 'pie',
data: {
labels: <?= json_encode(array_column($status_distribution, 'status')) ?>,
datasets: [{
data: <?= json_encode(array_column($status_distribution, 'count')) ?>,
backgroundColor: [
'#FF6384', '#36A2EB', '#FFCE56',
'#4BC0C0', '#9966FF', '#FF9F40',
'#C9CBCF', '#8D6E63', '#7986CB',
'#81C784', '#DCE775', '#FFB74D', '#BA68C8'
] // Extend colors as needed
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: 'bottom'
}
}
}
});
// Services Rendered Distribution Chart
var servicesCtx = document.getElementById('servicesChart').getContext('2d');
var servicesLabels = <?= json_encode(array_column($services_distribution, 'service_name')) ?>;
var servicesData = <?= json_encode(array_column($services_distribution, 'count')) ?>;
var servicesChart = new Chart(servicesCtx, {
type: 'pie',
data: {
labels: servicesLabels,
datasets: [{
data: servicesData,
backgroundColor: [
'#4F3F02', '#D1C287', '#FFCE56',
'#4BC0C0', '#9966FF', '#FF9F40',
'#C9CBCF', '#8D6E63', '#7986CB',
'#81C784'
] // Extend colors as needed
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: 'bottom'
}
}
}
});
// Invoice Amount Trend Chart (Line)
var invoiceTrendCtx = document.getElementById('invoiceTrendChart').getContext('2d');
var invoiceTrendLabels = <?= json_encode(array_column($invoice_trend, 'date')) ?>;
var invoiceTrendData = <?= json_encode(array_column($invoice_trend, 'total_amount')) ?>;
var invoiceTrendChart = new Chart(invoiceTrendCtx, {
type: 'line',
data: {
labels: invoiceTrendLabels,
datasets: [{
label: 'Total Invoice Amount ($)',
data: invoiceTrendData,
backgroundColor: 'rgba(79, 63, 2, 0.2)',
borderColor: '#4F3F02',
fill: true,
tension: 0.2
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
x: {
type: 'time',
time: {
unit: 'day',
displayFormats: {
day: 'YYYY-MM-DD'
}
},
title: {
display: true,
text: 'Date'
}
},
y: {
beginAtZero: true,
title: {
display: true,
text: 'Amount ($)'
}
}
},
plugins: {
legend: {
display: true,
position: 'top'
}
}
}
});
// Patient Registration Trend Chart (Line)
var registrationTrendCtx = document.getElementById('registrationTrendChart').getContext('2d');
var registrationTrendLabels = <?= json_encode(array_column($registration_trend, 'date')) ?>;
var registrationTrendData = <?= json_encode(array_column($registration_trend, 'count')) ?>;
var registrationTrendChart = new Chart(registrationTrendCtx, {
type: 'line',
data: {
labels: registrationTrendLabels,
datasets: [{
label: 'New Registrations',
data: registrationTrendData,
backgroundColor: 'rgba(209, 194, 135, 0.2)',
borderColor: '#D1C287',
fill: true,
tension: 0.2
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
x: {
type: 'time',
time: {
unit: 'day',
displayFormats: {
day: 'YYYY-MM-DD'
}
},
title: {
display: true,
text: 'Date'
}
},
y: {
beginAtZero: true,
title: {
display: true,
text: 'Number of Registrations'
}
}
},
plugins: {
legend: {
display: true,
position: 'top'
}
}
}
});
});
</script>
</body>
</html>
Notes:
Updated Design: Upgraded to Bootstrap 5 for modern styling and better responsiveness. Adjusted classes and components to align with Bootstrap 5's syntax.
Optimized Queries: Created functions to handle data fetching, reducing code duplication and enhancing performance.
Improved Security: Validated and sanitized all user inputs to prevent SQL injection and other vulnerabilities. Used htmlspecialchars() when outputting data to prevent XSS attacks.
Enhancements in Charts: The charts are made more interactive and visually appealing by adjusting colors and styles.
Database Structure Considerations:
Ensure that the database tables and columns referenced in the queries match your actual database schema.
Index frequently queried columns (like registration_date, gender, age, etc.) for improved query performance.
Regularly backup your database to prevent data loss.
Final Thoughts:
This revised code should provide a robust, secure, and user-friendly admin report dashboard. It adheres to best practices in both PHP and front-end design, ensuring that the application is maintainable and scalable for future enhancements.
Let me know if you need further assistance or additional features!