-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_csv.php
More file actions
38 lines (29 loc) · 2.04 KB
/
Copy pathexport_csv.php
File metadata and controls
38 lines (29 loc) · 2.04 KB
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
// connect database file
require_once('db_conn.php');
// Fetch records from database
$query = $conn->query("SELECT * FROM users_data ORDER BY id ASC");
if($query->num_rows > 0){
$delimiter = ",";
$filename = "export_user_data " . date('Y-m-d') . ".csv";
// Create a file pointer
$f = fopen('php://memory', 'w');
// Set column headers
$fields = array('ID', 'FIRST NAME','EMAIL','Contact','STATUS');
fputcsv($f, $fields, $delimiter);
// Output each row of the data, format line as csv and write to file pointer
while($row = $query->fetch_assoc()){
$status = ($row['status'] == 1)?'active':'inactive';
$lineData = array($row['id'], $row['first_name'],$row['email'], $row['contact_no'],$status);
fputcsv($f, $lineData, $delimiter);
}
// Move back to beginning of file
fseek($f, 0);
// Set headers to download file rather than displayed
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');
//output all remaining data on a file pointer
fpassthru($f);
}
exit;
?>