2012-01-06 62 views
0

嗨,我试图根据$ fratio对这个表进行排序。具有最高美元值的人将被置于表中的第一位。下面是我到目前为止工作的代码 - 你可以尝试使用基于一个特定的字段对php表进行排序

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

MYSQL * ORDER BY * – 2012-01-06 05:13:21

回答

2

// 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

+0

u能乌拉圭回合的建议.. – Swayam 2012-01-06 05:34:12

+0

是的,当然提供的编码。用SQL查询编辑评论。我希望你知道该把它放在哪里:) – MJA 2012-01-06 05:36:26

+0

我是一个PHP新手! Ur建议的完整编码版本会很棒! :) – Swayam 2012-01-06 05:47:24

0

你会在mysql这样的比例:

ORDER BY CEILING(kills/deaths) DESC 
相关问题