Upgrade to jQM 1.1.0
[busui.git] / labs / busdelay.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
46
47
48
49
50
51
52
<?php
 
setlocale(LC_CTYPE, 'C');
// source: http://stackoverflow.com/questions/81934/easy-way-to-export-a-sql-table-without-access-to-the-server-or-phpmyadmin#81951
include ('../include/common.inc.php');
$query = $conn->prepare('
SELECT * from myway_timingdeltas'
        , array(PDO::ATTR_CURSOR => PDO::FETCH_ORI_NEXT));
$query->execute();
$errors = $conn->errorInfo();
if ($errors[2] != "") {
    die("Export terminated, db error" . print_r($errors, true));
}
 
$headers = Array("date", "delay", "distance", "origin", "destination");
 
$fp = fopen('php://output', 'w');
if ($fp && $query) {
    //header('Content-Type: text/csv');
    header('Pragma: no-cache');
    header('Expires: 0');
    fputcsv($fp, $headers);
    while ($r = $query->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) {
        $row = Array();
        foreach ($headers as $i => $fieldName) {
            switch ($fieldName) {
                case "date":
                    $row[] = date("r",strtotime($r['date']." ".$r['time']));
                    break;
                case "delay":
                    $row[] = $r['timing_delta'];
                    break;
                case "distance":
                    $row[] = $r['stop_sequence'];
                    break;
                case "origin":
                    $row[] = $r['myway_stop'];
                    break;
                case "destination":
                    $row[] = $r['route_name'];
                    break;
                default:
                    break;
            }
        }
        fputcsv($fp, array_values($row));
    }
    die;
}
?>