-1

I am wondering how to get the rank of a player who was wrote to my page using:

    $res = $con->query("SELECT * FROM hiscores ORDER BY `0` DESC LIMIT 50");

I have a list of my players on my page organized by their values in column 0. How do I get what rank the player is based on the ORDER BY?

Nic
  • 98
  • 2
  • 11

1 Answers1

0

You should ORDER BY score DESC

Then something like:

foreach ($res as $player) {
    echo "Rank #"
       . (intval(key($player)) + 1)
       . " for player "
       . $player['playerName']
       . "<br>" . PHP_EOL;
}

The logic is to take the element's index and increment it by one

$player[0] = 1
$player[1] = 2
...
Daniel W.
  • 31,164
  • 13
  • 93
  • 151
  • I got ranking working so it displays what rank they are ordered by their XP. But how I do I fetch that rank later on in the program? – Nic Oct 30 '13 at 14:03
  • ` $rank=0; $temp_score=0;if($temp_score!=$row['0']) $rank++;` $rank – Nic Oct 30 '13 at 14:04
  • Don't use `if` without brackets :-S If you need to fetch the exact rank of someone, you should put up a cronjob running every few minutes to update a column "rank" in your table, by the help of the above script. – Daniel W. Oct 30 '13 at 14:10