0

What would be the logic on scores page to get:

  • current user order among all players (like #115): MySql SELECT for all users ORDER BY score DESC and then check where is the current user?

  • better two scores: Get current user and then SELECT LIMIT 2 WHERE score > "$currentuserscore"?

  • less two scores: Get current user and then SELECT LIMIT 2 WHERE score < "$currentuserscore"?

I feel this is very buggy, is there any other way?

xyro
  • 465
  • 1
  • 6
  • 25

1 Answers1

1

For option 1:

select count(1) as count from users where score>=$user_score

This should give you the person's rank. If you worried about multiple people having the same score as the current user change the >= to a > then you can add 1 to the result.

For option 2 and 3:

select * from users where score>$user_score order by score asc limit 2

select * from users where score<$user_score order by score desc limit 2

Or you could get all 5 (2 before, 2 after and current user) in one query once you have the rank from first query:

$start = $user_rank - 2;
$query = "select * from users order by score asc limit {$start},5";

Or if you didn't want current user or anyone with the same score as the current user to be included you could do this:

$start = $user_rank - 2;
$query = "select * from users where score!=$user_score order by score asc limit {$start},4";
Pitchinnate
  • 7,517
  • 1
  • 20
  • 37