使用php和mysql添加排名

时间:2017-10-10 09:53:17

标签: php mysql

我有一些用于从我的数据库中提取结果的PHP代码:

<?php  $res=mysql_query("SELECT SUM(step_count.steps) as total, 
leagues.league_id, leagues.league_name
    FROM step_count
    INNER JOIN logins on
      step_count.email=logins.email
    INNER JOIN leagues ON
      leagues.email=logins.email
    GROUP BY leagues.league_id, leagues.league_name
    ORDER BY `total` DESC LIMIT 10");

    while ($row = mysql_fetch_array($res))
    {
      ?>

      <?php echo $row['league_name']; ?>
      <?php echo $row['total']; ?>

我得到了想要的结果

Test      | 6200 
TestTwo   | 5600 
TestThree | 3400

我想知道是否有办法为这些结果创建排名,所以它看起来像这样,而不必自己手动添加排名

#1 Test      | 6200 
#2 TestTwo   | 5600 
#3 TestThree | 3400

1 个答案:

答案 0 :(得分:0)

根据您的问题,您想要显示列数,请尝试此代码

<?php  
    $res=mysql_query("SELECT SUM(step_count.steps) as total, 
    leagues.league_id, leagues.league_name
        FROM step_count
        INNER JOIN logins on
          step_count.email=logins.email
        INNER JOIN leagues ON
          leagues.email=logins.email
        GROUP BY leagues.league_id, leagues.league_name
        ORDER BY `total` DESC LIMIT 10 ");
        $rank = 1;
        while ($row = mysql_fetch_array($res))
        {
    ?>
    <?php echo '#'.$rank++; ?>
    <?php echo $row['league_name']; ?>
    <?php echo $row['total']; ?>
<?php } ?>