-
Notifications
You must be signed in to change notification settings - Fork 7
/
getJSONHighScores_example.php
52 lines (36 loc) · 1.18 KB
/
getJSONHighScores_example.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
//Connect to DB
require("../connect.php");
$result = pg_query($con, "SELECT json_build_object('Name', name, 'Time', time) FROM (SELECT name, time FROM _Table_ WHERE difficulty = 0 ORDER BY time, id LIMIT 10) _Table_" );
if (!$result) {
echo "An error occurred.\n";
exit;
}
header('Content-type:application/json;charset=utf-8');
$json = '{"highscores":';
$json .= '{"easy": [';
while ($row = pg_fetch_array($result))
{
$json .= $row[0] . ",";
}
$json = trim($json, ",");
$json .= " ],";
$result = pg_query($con, "SELECT json_build_object('Name', name, 'Time', time) FROM (SELECT name, time FROM _Table_ WHERE difficulty = 1 ORDER BY time, id LIMIT 10) _Table_" );
$json .= '"medium": [';
while ($row = pg_fetch_array($result))
{
$json .= $row[0] . ",";
}
$json = trim($json, ",");
$json .= " ],";
$result = pg_query($con, "SELECT json_build_object('Name', name, 'Time', time) FROM (SELECT name, time FROM _Table_ WHERE difficulty = 2 ORDER BY time, id LIMIT 10) _Table_" );
$json .= '"hard": [';
while ($row = pg_fetch_array($result))
{
$json .= $row[0] . ",";
}
$json = trim($json, ",");
$json .= " ]";
$json .= "}}";
echo $json;
?>