-
Notifications
You must be signed in to change notification settings - Fork 0
/
highscores.php
26 lines (26 loc) · 917 Bytes
/
highscores.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
<?php
if (!isset($_REQUEST['action'])) {die();}
$db = new PDO();
if ($_REQUEST['action'] == "add") {
$add = $db->prepare('INSERT INTO highscores VALUES (?, ?, ?)');
$add->execute(array( $db->quote($_POST['id']), $db->quote($_POST['name']), intval($_POST['points']) ));
$add->closeCursor();
}
if ($_REQUEST['action'] == "get") {
$highscores = $db->query('SELECT id,name,points FROM highscores ORDER BY points DESC');
$topTenPlusMine = array();
foreach ($highscores->fetchAll() as $i=>$row) {
if ($i<10 || ($row['id']==$_GET['id'] && $row['name']==$_GET['name'])) {
$topTenPlusMine[] = array(
'position'=>$i+1,
'name'=>substr($row['name'],1,-1),
'points'=>$row['points'],
'me'=>(substr($row['id'],1,-1)==$_GET['id'] && substr($row['name'],1,-1)==$_GET['name'])
);
}
}
header('Content-Type: application/json');
echo json_encode($topTenPlusMine);
die();
}
?>