仅根据一个特定字段对 php 表进行排序
您好,我正在尝试根据 $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);
?>
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);
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用
因此完整的查询将如下所示
完整的代码将如下所示:
这是“ORDER BY”的示例。您确实应该考虑至少阅读本文以了解其主要思想。
http://www.w3schools.com/php/php_mysql_order_by.asp
You could try using
So the full query would look like
And the full code would look like this:
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
你将在 mysql 中设置比率,如下所示:
you will make the ratio in mysql like :