-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_warehouse_csv.php
53 lines (44 loc) · 1.63 KB
/
export_warehouse_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
50
51
52
53
<?php
include("./layouts/session.php"); // include session
include 'conn.php'; // Include database connection
// Establish the connection
$conn = connectMainDB();
// User's email
$user_email = htmlspecialchars($_SESSION['email']);
// Set headers to download file as CSV
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=Warehouse_Report.csv');
// Open output stream
$output = fopen('php://output', 'w');
// Define and write column headers
fputcsv($output, ['#', 'Name', 'Contact Person', 'Phone', 'Total Products',
'Country', 'State', 'Email', 'Created On', 'Status']);
// Fetch data from the warehouse table
$sql = "SELECT * FROM warehouse WHERE user_email = '$user_email'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$i = 1;
while ($row = $result->fetch_assoc()) {
// Get total products count for each warehouse
$warehouse_name = $row['name'];
$product_count_sql = "SELECT COUNT(*) as total_products FROM products WHERE warehouse = '$warehouse_name' AND email = '$user_email'";
$product_count_result = $conn->query($product_count_sql);
$total_products = $product_count_result->fetch_assoc()['total_products'];
// Write each row to CSV
fputcsv($output, [
$i++,
$row['name'],
$row['contact_person'],
$row['phone'],
$total_products,
$row['country'],
$row['state'],
$row['email'],
date('d M Y', strtotime($row['timestamp'])),
$row['status']
]);
}
}
// Close the output stream
fclose($output);
exit;