简体   繁体   中英

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

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") 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++;
}

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 .

用它:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM