This repository has been archived by the owner on May 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dengue.php
83 lines (71 loc) · 2.26 KB
/
dengue.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
function fetchDengueYearDataInJson() {
global $mysqlhost, $mysqlport, $mysqluser, $mysqlpass, $mysqldb;
$obj = new PHP2MySQL($mysqlhost, $mysqlport, $mysqldb, "dengue", $mysqluser, $mysqlpass);
$getRes = $obj -> execsql(
"select year, sum(dengueval) as dengueval from dengue where year in (select * from (select year from dengue group by year order by year desc limit 6) as yLs) group by year order by year asc;",
True,
array()
);
if($getRes["state"] == "success") {
return $getRes["data"];
} else {
return array();
}
}
function fetchDengueMonthDataInJson() {
global $mysqlhost, $mysqlport, $mysqluser, $mysqlpass, $mysqldb;
$obj = new PHP2MySQL($mysqlhost, $mysqlport, $mysqldb, "dengue", $mysqluser, $mysqlpass);
$getRes = $obj -> execsql(
"select * from dengue where year in (select * from (select year from dengue group by year order by year desc limit 4) as yLs) order by year asc, month asc;",
True,
array()
);
if($getRes["state"] == "success") {
return $getRes["data"];
} else {
return array();
}
}
# array data to desired format
function Array2CSV($data,$format) {
$output = ["year","month","dengueval"];
$split = ($format == "csv" ? "," : "\t");
echo join($split, $output)."\n";
for($i = 0 ; $i < count($data); $i++) {
$output = [$data[$i]["year"], $data[$i]["month"], $data[$i]["dengueval"]];
echo join($split, $output)."\n";
}
}
# output as json
switch($_GET['v']) {
default:
case "a1":
header('Content-Type: application/json');
echo json_encode(fetchDengueYearDataInJson());
break;
case "a2":
header('Content-Type: application/json');
echo json_encode(fetchDengueMonthDataInJson());
break;
case "dev1":
if(array_key_exists("f",$_GET)) {
switch(strtolower($_GET["f"])) {
default:
case "json":
header('Content-Type: application/json');
echo json_encode(fetchDengueMonthDataInJson());
break;
case "csv":
case "txt":
header('Content-Type: text/txt');
echo Array2CSV(fetchDengueMonthDataInJson(), strtolower($_GET["f"]));
break;
}
} else {
header('Content-Type: application/json');
echo json_encode(fetchDengueMonthDataInJson());
}
break;
}
?>