-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_units_csv.php
49 lines (39 loc) · 1.67 KB
/
export_units_csv.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
include 'conn.php'; // Database connection
// Start session and get user email
session_start();
$user_email = $_SESSION['email'];
// Fetch the data
$conn = connectMainDB();
$sql = "SELECT * FROM units WHERE user_email = '$user_email' ORDER BY name";
$result = $conn->query($sql);
// Set the headers to force download as CSV
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=units_report.csv');
// Open the output stream
$output = fopen('php://output', 'w');
// Write the column headers
fputcsv($output, ['Unit', 'Short Name', 'No. of Products', 'Created On', 'Status']);
// Fetch the data from the result and write each row to the CSV
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
// Get unit name and short name
$unit_name = $row['name'];
$unit_short_name = $row['short_name'];
// Count the total products for each unit
$product_count_sql = "SELECT COUNT(*) as total_units FROM products WHERE unit = '$unit_short_name' AND email = '$user_email'";
$product_count_result = $conn->query($product_count_sql);
$product_count_row = $product_count_result->fetch_assoc();
$total_units = $product_count_row['total_units'];
// Format the created on date
$created_on = date('d M Y', strtotime($row['timestamp']));
$status = ucfirst($row['status']); // Capitalize status
// Write data to CSV
fputcsv($output, [$unit_name, $unit_short_name, $total_units, $created_on, $status]);
}
} else {
// If no data is available, add a row to the CSV file
fputcsv($output, ['No data available']);
}
// Close the output stream
fclose($output);