CSV Download and Notes highlight
This commit is contained in:
47
download.php
Normal file
47
download.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
include("functions.php");
|
||||
require_db_auth();
|
||||
|
||||
// Create connection
|
||||
$conn = new mysqli($host, $username, $password, $database);
|
||||
|
||||
// Check connection
|
||||
if ($conn->connect_error) {
|
||||
die("Connection failed: " . $conn->connect_error);
|
||||
}
|
||||
|
||||
$year = $_GET['year'] ?? date('Y');
|
||||
$month = $_GET['month'] ?? date('n');
|
||||
|
||||
$datetime = DateTime::createFromFormat('!m', $month);
|
||||
$monthName = $datetime->format('F');
|
||||
$filename = $monthName . $year . '.csv';
|
||||
|
||||
header('Content-Type: text/csv');
|
||||
header('Content-Disposition: attachment;filename=' . $filename);
|
||||
|
||||
$output = fopen('php://output', 'w');
|
||||
|
||||
$escape = '\\'; // Define the escape character
|
||||
|
||||
// Fetch data
|
||||
$sql = "SELECT * FROM submitted WHERE status != 'DELETED' and MONTH(eta) = $month and YEAR(eta) = $year ORDER BY eta ASC;";
|
||||
$result = $conn->query($sql);
|
||||
|
||||
// Output column headers
|
||||
$fields = $result->fetch_fields();
|
||||
$headers = [];
|
||||
foreach ($fields as $field) {
|
||||
$headers[] = $field->name;
|
||||
}
|
||||
fputcsv($output, $headers, ',', '"', $escape);
|
||||
|
||||
// Output rows
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
fputcsv($output, $row, ',', '"', $escape);
|
||||
}
|
||||
|
||||
// Close the database connection
|
||||
$conn->close();
|
||||
fclose($output);
|
||||
?>
|
||||
Reference in New Issue
Block a user