-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_suppliers_csv.php
45 lines (35 loc) · 1.34 KB
/
export_suppliers_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
<?php
include("./layouts/session.php"); // include session
include 'conn.php'; // Include database connection
// Establish the connection
$conn = connectMainDB();
$user_email = htmlspecialchars($_SESSION['email']); // User's email
// Fetch supplier data
$stmt = $conn->prepare("SELECT id, name, rc_code, email, phone, city FROM suppliers WHERE user_email = ?");
$stmt->bind_param("s", $user_email);
$stmt->execute();
$result = $stmt->get_result();
// Set headers to force the file download as CSV
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="Supplier_List.csv"');
// Open the output stream to write CSV data
$output = fopen('php://output', 'w');
// Add the header row for CSV
fputcsv($output, ['#', 'Supplier Name', 'RC Code', 'Email', 'Phone', 'City']);
// Initialize a counter for sequential numbering
$counter = 1;
// Loop through results and write them as CSV rows
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
// Use the counter for numbering, then increment it
fputcsv($output, [$counter++, $row['name'], $row['rc_code'], $row['email'], $row['phone'], $row['city']]);
}
} else {
// If no data is found, output a message
fputcsv($output, ['No suppliers found']);
}
// Close the output stream
fclose($output);
// Close the database connection
$conn->close();
?>