2017-07-17 95 views
0

更新,以提供更好的解釋最高級別的PHP MYSQL(在RuneScape宗族榜):順序按

我在試圖創造一個高分功能爲我的家族在一個遊戲叫在RuneScape不過我還是要說的過程很「知道了如何通過‘總水平’

Working Demo without sorting by total level

例如silenthawk排行榜的排序應該是頂部,因爲他一共有1941。我與列ID,等級和playername

我正在做一個SQL查詢來獲取所有的氏族的人,然後通過使用一種叫做oldschoolhiscore頁面下車在RuneScape網站高分的表稱爲rsusers .php,我發現在github上(linked here) - 我想我所要做的就是編輯這個頁面,按照'總計'排序,但我不確定?

這裏是非常精簡和簡單的演示和代碼:

Demo here

代碼:

 <?php 

    require 'OldSchoolHiscore.php'; 


    $dbhost = 'localhost'; 
    $dbuser = 'xxxx'; 
    $dbpass = 'xxxxxxxxx'; 
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); 
    $dbname = 'xxxxxx'; 
    $connection = mysql_select_db($dbname); 


    $sql = "SELECT * FROM rsusers"; 
    $rs_result = mysql_query ($sql); 


    ?> 



     <table class="table table-striped table-list-search"> 
      <thead> 
       <tr> 
        <td>Name</td> 
        <td>Level</td> 
       </tr> 
      </thead> 

      <tbody> 


      <?php 

      while ($row = mysql_fetch_assoc($rs_result)) { 



       $hs = new OldSchoolHiscore($row['username']); 


       $total_level = $hs->getSkillLevel('overall'); 

       echo "<tr>"; 

       echo "<td>".$row['username']."</td>"; 
       echo "<td>$total_level</td>"; 


       echo "</tr>"; 




      } 

      ?> 

    </tbody> 
    </table> 

SQL

INSERT INTO rsusers 
(username, clanrank) 
VALUES 
('Dwh_Hunters', 'lieutenant'), 
('SilentHawk', 'general'), 
('Sir Novel', 'lieutenant'), 
('Ryuk Yagami', 'general'); 
+0

見https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to -be-a-very-simple-sql-query – Strawberry

回答

0

使用ORDER BY

嘗試

$sql = ('SELECT * FROM rsusers ORDER BY level DESC'); 
+0

你可能想要添加一些頭部排序功能。檢查這樣的事情。 http://phppot.com/php/sorting-mysql-column-using-php/ –

+0

我沒有列級別,它從表中獲取每個用戶名,然後在查詢後獲取相關信息。 –

+0

@ChristopherWard然後將所有內容放入一個二維數組中,並使用'usort()'按'level'字段對數組進行排序。 – Barmar