嗨,我正试图根据$fratio对这个表进行排序。拥有最高$fratio的人将被列为表中的第一名。这是我迄今为止工作过的代码-
// 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);
?>
您可以尝试使用
ORDER BY kills / deaths DESC
所以完整的查询看起来像
SELECT `UserID`,`Playername`,`Kills`,`Deaths` FROM users ORDER BY kills / deaths DESC LIMIT 0,50
完整的代码看起来是这样的:
// 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);
?>
下面是一个"ORDERBY"的例子。你真的应该考虑至少阅读这篇文章来了解它的大意
http://www.w3schools.com/php/php_mysql_order_by.asp
您将在mysql中设置如下比率:
ORDER BY CEILING( kills / deaths ) DESC