-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv.php
37 lines (32 loc) · 1.04 KB
/
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
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
//load field names
$field_list = file('db_fields.txt', FILE_IGNORE_NEW_LINES);
//Shift out the first three rows - not needed in output file
array_shift($field_list);
array_shift($field_list);
array_shift($field_list);
array_shift($field_list);
$fields = implode(',',$field_list);
//database query
require_once('db.php');
$sql = "SELECT $fields FROM matches ORDER BY team, matchnum";
//$sql = "SELECT * FROM matches WHERE ID IN (SELECT MAX(ID) FROM matches GROUP BY matchnum, team)";
$result = mysqli_query($conn,$sql);
$filename = 'matches.csv';
$f = fopen('php://memory', 'w');
//header rows
//change first header row, because starting a CSV with "ID" makes excel throw a filetype error (??)
$field_list[0] = "match_ID";
fputcsv($f, $field_list);
//data rows
while ($row=mysqli_fetch_assoc($result)){
fputcsv($f,$row);
}
fseek($f, 0);
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="'.$filename.'";');
fpassthru($f);
mysqli_close($conn);
?>