function getUserRankInGame($userId, $gameId) { $results = GetGameResults($gameId); $i = 1; while ($r = $results->fetch_assoc()) { if ($r["PlayerId"] == $userId) { break 1; } $i += 1; } if ($i == 1) { return "1st"; } else { if ($i == 2) { return "2nd"; } else { if ($i == 3) { return "3rd"; } else { return $i . "th"; } } } }
<?php include 'functions.php'; $message = array(); /*message to be returned*/ $gameId = $_POST['gameId']; /*the game being played */ $results = GetGameResults($gameId); while ($r = $results->fetch_assoc()) { $row = array(); $row["Name"] = GetPlayersName($r["PlayerId"]); $row["Gold"] = $r["Gold"]; $message['Results'][] = $row; } echo json_encode($message); /*return the message as ajson encoded object*/