仅根据一个特定字段对 php 表进行排序

发布于 2024-12-25 08:16:28 字数 1310 浏览 0 评论 0原文

您好,我正在尝试根据 $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘心安 2025-01-01 08:16:28

您可以尝试使用

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);
  ?>

这是“ORDER BY”的示例。您确实应该考虑至少阅读本文以了解其主要思想。

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

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

℉絮湮 2025-01-01 08:16:28

你将在 mysql 中设置比率,如下所示:

ORDER BY CEILING( kills / deaths ) DESC

you will make the ratio in mysql like :

ORDER BY CEILING( kills / deaths ) DESC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文