Mini Kabibi Habibi

Current Path : C:/xampp/htdocs/clinic/
Upload File :
Current File : C:/xampp/htdocs/clinic/export_health_records.php

<?php
session_start();
include 'includes/db.php';

if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    header("Location: login.php");
    exit();
}

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=health_records.csv');

$output = fopen('php://output', 'w');

// Column headers
fputcsv($output, ['Date', 'Patient Name', 'School', 'Age', 'Chief Complaint', 'BP', 'CR', 'RR', 'O2 Sat', 'Temp', 'Physical Exam', 'Diagnosis', 'Treatment']);

$stmt = $pdo->prepare("
    SELECT 
        l.patient_name,
        l.school,
        l.age,
        hr.date,
        hr.chief_complaint,
        hr.findings_bp,
        hr.findings_cr,
        hr.findings_rr,
        hr.findings_o2sat,
        hr.findings_temp,
        hr.physical_examination,
        hr.diagnosis,
        hr.treatment
    FROM logs l
    INNER JOIN health_records hr ON hr.log_id = l.id
    ORDER BY hr.date DESC
");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($rows as $row) {
    fputcsv($output, [
        date('m/d/Y', strtotime($row['date'])),
        $row['patient_name'],
        $row['school'],
        $row['age'],
        $row['chief_complaint'],
        $row['findings_bp'],
        $row['findings_cr'],
        $row['findings_rr'],
        $row['findings_o2sat'],
        $row['findings_temp'],
        $row['physical_examination'],
        $row['diagnosis'],
        $row['treatment'],
    ]);
}

fclose($output);
exit();