2017-10-10 85 views
-1

我拉離我的數據庫結果一些PHP代碼添加排名:使用PHP和MySQL

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

你的意思是總 –

+0

它已經由總排序排序,我只是想它是什麼參數做加入下一個排名得分它 – Phoebe

+0

你想排名?如果它是現有的,你可以添加一個ORDER語句,或者如果你想要某種顯示順序,你可以添加一個顯示順序列... – Odyssey1111

回答

1

由於p呃你要顯示的列數你的問題試試這個代碼

<?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 } ?>