-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-ranking.php
More file actions
27 lines (26 loc) · 972 Bytes
/
get-ranking.php
File metadata and controls
27 lines (26 loc) · 972 Bytes
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
<?php
// Restituisce le prime $limit entry in classifica.
function get_ranking($connection_string, $limit)
{
$ret_val = false;
$db = pg_connect($connection_string) or die('Impossibile connettersi al database!');
// La seguente query SQL fornisce le prime $limit entry della tabella
// contenente i punteggi massimi (hiscore) per ciascun utente (GROUP BY nickname).
$sql = 'SELECT nickname, MAX(score) AS hiscore
FROM (SELECT * FROM "game" INNER JOIN "user" ON "game".email = "user".email)
GROUP BY nickname
ORDER BY hiscore DESC
LIMIT $1';
$result = pg_prepare($db, "Get-Ranking", $sql);
if ($result) {
$result = pg_execute($db, "Get-Ranking", array($limit));
if ($result) {
$ret_val = array();
while ($row = pg_fetch_assoc($result)) {
array_push($ret_val, $row);
}
}
}
pg_close($db);
return $ret_val;
}