0

Hi I am trying to sort this table on the basis of $fratio. The one who has the highest $fratio will be placed as Number 1 in the table. Here's the code that I have worked so far -

// MySQL connection.
$connection = mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database. Be sure the databasename exists and online is.");
$query="SELECT `UserID`,`Playername`,`Kills`,`Deaths` FROM users LIMIT 0,50"; 
$query = mysql_query($query);

echo('<table width="300" border="2" cellspacing="3" cellpadding="3">
<tr>
    <td style="min-width:150px;">Playername:</td>
    <td style="width:100px">Kills:</td>
    <td style="width:100px">Deaths:</td>
    <td style="width:100px">Ratio:</td>
</tr>');
while($row = mysql_fetch_assoc($query))
{
$id = $row['UserID'];
$playername = $row['Playername'];
$kills = $row['Kills'];
$deaths = $row['Deaths'];
$ratio = ($kills/$deaths);
$fratio = ceil($ratio); 

echo('
  <tr>
    <td style="min-width:150px;"><a href="stats.php?id='.$id.'">'.$playername.'</a></td>
    <td style="width:100px">'.$kills.'</td>
    <td style="width:100px">'.$deaths.'</td>
    <td style="width:100px">'.$fratio.'</td>
  </tr>');
  }
  echo('</table>');

  mysql_close($connection);
  ?>
Swayam
  • 25
  • 6

2 Answers2

2

You could try using

ORDER BY kills / deaths DESC

So the full query would look like

SELECT `UserID`,`Playername`,`Kills`,`Deaths` FROM users ORDER BY kills / deaths DESC LIMIT 0,50

And the full code would look like this:

// MySQL connection.
$connection = mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database. Be sure the databasename exists and online is.");
$query="SELECT `UserID`,`Playername`,`Kills`,`Deaths` FROM users ORDER BY kills / deaths LIMIT 0,50"; 
$query = mysql_query($query);

echo('<table width="300" border="2" cellspacing="3" cellpadding="3">
<tr>
    <td style="min-width:150px;">Playername:</td>
    <td style="width:100px">Kills:</td>
    <td style="width:100px">Deaths:</td>
    <td style="width:100px">Ratio:</td>
</tr>');
while($row = mysql_fetch_assoc($query))
{
$id = $row['UserID'];
$playername = $row['Playername'];
$kills = $row['Kills'];
$deaths = $row['Deaths'];
$ratio = ($kills/$deaths);
$fratio = ceil($ratio); 

echo('
  <tr>
    <td style="min-width:150px;"><a href="stats.php?id='.$id.'">'.$playername.'</a></td>
    <td style="width:100px">'.$kills.'</td>
    <td style="width:100px">'.$deaths.'</td>
    <td style="width:100px">'.$fratio.'</td>
  </tr>');
  }
  echo('</table>');

  mysql_close($connection);
  ?>

Here's an example of the "ORDER BY". You should really consider reading atleast this to get the main idea of it.

http://www.w3schools.com/php/php_mysql_order_by.asp

  • Yeah, sure. Edited the comment with the SQL query. I hope you know where to place it :) –  Jan 06 '12 at 05:36
  • I am a PHP newbie! A full coded version of Ur suggestion would be great! :) – Swayam Jan 06 '12 at 05:47
0

you will make the ratio in mysql like :

ORDER BY CEILING( kills / deaths ) DESC
jogesh_pi
  • 9,477
  • 4
  • 31
  • 61