-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.php
More file actions
52 lines (39 loc) · 1.27 KB
/
export.php
File metadata and controls
52 lines (39 loc) · 1.27 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
//A PHP script to export the current databse as a csv file
//include database configuration file
include 'config.php';
//SQL query to retreive all intentiondata data by ascending ID
$query = "SELECT * FROM intentiondata ORDER BY ID ASC";
//delimiter for the CSV format
$delimiter = ",";
$filename = "intentionroundingdata.csv";
//create a file pointer
$f = fopen('php://memory', 'w');
//set column headers for csv file
$categories = array(
"ID",
"date",
"nurse",
"consultant",
"bed",
"type",
"value",
);
fputcsv($f, $categories, $delimiter);
//if loop to continue to while loop if connects
if($result = mysqli_query($link, $query)){
//while loop to go through the database and pull data and insert it into the csv file
while($row = mysqli_fetch_array($result)){
$lineData = array($row['ID'], $row['date'], $row['nurse'], $row['consultant'], $row['bed'], $row['type'], $row['value']);
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;
?>