开发者

How to get the player rank from there stats in php and mysql

开发者 https://www.devze.com 2023-01-02 03:17 出处:网络
I have the following code i am trying to get the play speed rank from mysql. Mysql Table Called Accounts

I have the following code i am trying to get the play speed rank from mysql.

Mysql Table Called Accounts

 Username | Speed
 -----------------
 Player1     21
 Player2     52
 Player3     33   

 Player2 52(Ranked:1)
 Player3 33(Ranked:2)
 Player2 21(Ranked:3)

 $result = mysql_query("SELECT * FROM accounts")开发者_StackOverflow or die (mysql_error());
 while($row = mysql_fetch_array($result)) {
          $username= $row[username];
 $speed = $row['speed'];
  }

sorry i have been trying different ways but i cant get it to work


Why not simply:

$result = mysql_query("SELECT * FROM accounts ORDER BY speed DESC") or die (mysql_error());

$rank = 1;
while($row = mysql_fetch_array($result)) {
    $username= $row[username];
    $speed = $row['speed'];
    $rank++;
}


Use it:

$result = mysql_query("SELECT * FROM accounts ORDER BY speed DESC") or die (mysql_error());


Just sort them by speed:

$result = mysql_query("SELECT * FROM accounts ORDER BY speed DESC") or die (mysql_error());
$prev_rank = 0;
 while($row = mysql_fetch_array($result)) {
          $username= $row['username'];
          $speed = $row['speed'];
          $rank = ++$prev_rank;
  }

Read about ORDER BY.

0

精彩评论

暂无评论...
验证码 换一张
取 消