-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpiry_product_csv.php
39 lines (29 loc) · 1009 Bytes
/
expiry_product_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
<?php
include("./layouts/session.php");
// Include database connection
require('conn.php');
$conn = connectMainDB();
// Sanitize email (for safety)
$email = trim($conn->real_escape_string($_SESSION['email']));
// Check for expired products
$query = "SELECT * FROM expired_products WHERE email = '$email'";
$result = $conn->query($query);
// Set headers to indicate that the output is a CSV file
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename="expired_products.csv"');
// Create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
// Add column headers
fputcsv($output, ['Product Name', 'Store', 'SKU', 'Manufactured Date', 'Expiry Date']);
// Fetch data and write it to the CSV
while ($row = $result->fetch_assoc()) {
fputcsv($output, [
$row['product_name'],
$row['store'],
$row['sku'],
$row['manufactured_date'],
$row['expiry_date']
]);
}
// Close the file pointer
fclose($output);