-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaderboard_action.php
84 lines (71 loc) · 2.16 KB
/
leaderboard_action.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
84
<?php
//leaderboard_action.php
// include database
include('database/database_connection.php');
session_start();
if(isset($_POST["action"]))
{
// for user list
if($_POST["action"] == "fetch")
{
//retrieve data to display leaderboard with users with their username, highscore and average score ordered by score_value
$query = "
SELECT DISTINCT tbl_user.user_id FROM tbl_user
INNER JOIN tbl_score
ON tbl_score.user_id = tbl_user.user_id
";
// for searching
if(isset($_POST["search"]["value"]))
{
$query .= '
WHERE tbl_score.score_value LIKE "%'.$_POST["search"]["value"].'%"
OR tbl_user.user_name LIKE "%'.$_POST["search"]["value"].'%"
';
}
// for ordering data
//not working because properly because the query data is different from the data array(elements of data array dont exist in the query generated using sql)
if(isset($_POST["order"]))
{
$query .= '
ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].'
';
}
else
{
$query .= '
ORDER BY tbl_score.score_value DESC
';
}
if($_POST["length"] != -1)
{
$query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}
// execute and fetch matched rows
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$data = array();
$filtered_rows = $statement->rowCount();
// for each row put data in array
foreach($result as $row)
{
$sub_array = array();
$sub_array[] = get_rank($connect,$row["user_id"]);
$sub_array[] = get_user_name($connect,$row["user_id"]);
$sub_array[] = get_highscore($connect,$row["user_id"]);
$sub_array[] = average_score($connect,$row["user_id"]);
$data[] = $sub_array;
}
sort($data);
// store in output
$output = array(
"draw" => intval($_POST["draw"]),
"recordsTotal" => get_total_records($connect, 'tbl_score'),
"recordsFiltered" => $filtered_rows,
"data" => $data
);
// return output data in json format
echo json_encode($output);
}
}
?>