doupeng8494 2012-01-06 05:09
浏览 54

基于一个特定字段对php表进行排序

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);
  ?>
  • 写回答

2条回答 默认 最新

  • duanji1043 2012-01-06 05:13
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像