nemp Posted February 28, 2013 Share Posted February 28, 2013 (edited) Hello! I'm currently trying to make my own highscore to the mmo game Tibia (www.tibia.com) and gather all the player's levels and sort them into a table. I have found out how to sort them by the level, but here's my problem: every player should have a rank. And I want the rank to be assigned to the highest level. For example if we have 5 players. Player 1 = Level 40 Player 2 = Level 90 Player 3 = Level 100 Player 4 = Level 50 Player 5 = Level 20 And it would assign the ranks like this: Rank 1 = Player 3 Rank 2 = Player 2 Rank 3 = Player 4 Rank 4 = Player 1 Rank 5 = Player 5 But I can't find out how to sort them by Rank & Level. Does it have to do with the database or something? I'm really new at this and I really need help. The highest level should have the highest rank automatically. Where highest rank = 1, and it goes on up to 1000. So a list of the top 1000 levels should be displayed on my website. Currently I'm just trying out to see an example list of 5 players that I put into the database. Here's my lines of codes to display them: <?php $result = mysql_query("SELECT rank, name, level, vocation, server FROM topboard ORDER BY level DESC LIMIT 0, 1000"); while ($row=mysql_fetch_array($result, MYSQL_ASSOC) or die(mysql_error())) { echo "<tr>"; echo "<td size='10%'>" . $row['rank'] . "</td>"; echo "<td size='50%'>" . $row['name'] . "</td>"; echo "<td size='10%'>" . $row['level'] . "</td>"; echo "<td size='10%''>" . $row['vocation'] . "</td>"; echo "<td size='20%'>" . $row['server'] . "</td>"; echo "</tr>"; } echo "</table>"; ?> Here's a screenshot showing what it looks like right now: Edited February 28, 2013 by nemp Quote Link to comment https://forums.phpfreaks.com/topic/275035-need-help-with-a-table-in-php-sorting-by-id/ Share on other sites More sharing options...
requinix Posted February 28, 2013 Share Posted February 28, 2013 (edited) Don't use $row['rank'] and just count it out yourself. $rank = 1; // while { // print everything using $rank as the rank $rank++; // } Edited February 28, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/275035-need-help-with-a-table-in-php-sorting-by-id/#findComment-1415515 Share on other sites More sharing options...
AyKay47 Posted February 28, 2013 Share Posted February 28, 2013 (edited) Adding to what requinix posted, it looks like you are using the `rank` column as a unique index when it should not be (as requinix stated above it does not belong in the table at all). Instead use that field as a unique identifier of each user in the table and name it something generic like `ID`. Edited February 28, 2013 by AyKay47 Quote Link to comment https://forums.phpfreaks.com/topic/275035-need-help-with-a-table-in-php-sorting-by-id/#findComment-1415517 Share on other sites More sharing options...
nemp Posted February 28, 2013 Author Share Posted February 28, 2013 So I should remove rank from the database as well or what? And where should I put it in the code? Could you add it to the code I have above? I'm really new at this. I think it would be: <?php $result = mysql_query("SELECT rank, name, level, vocation, server FROM topboard ORDER BY level DESC LIMIT 0, 1000"); $rank = 1; while ($row=mysql_fetch_array($result, MYSQL_ASSOC) or die(mysql_error())) { echo "<tr>"; echo "<td size='10%'>" . $rank . "</td>"; echo "<td size='50%'>" . $row['name'] . "</td>"; echo "<td size='10%'>" . $row['level'] . "</td>"; echo "<td size='10%''>" . $row['vocation'] . "</td>"; echo "<td size='20%'>" . $row['server'] . "</td>"; echo "</tr>"; $rank++; } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/275035-need-help-with-a-table-in-php-sorting-by-id/#findComment-1415539 Share on other sites More sharing options...
requinix Posted February 28, 2013 Share Posted February 28, 2013 Looks right to me. So I should remove rank from the database as well or what?Depends. What is it supposed to mean? What is it used for and where does it come from? Quote Link to comment https://forums.phpfreaks.com/topic/275035-need-help-with-a-table-in-php-sorting-by-id/#findComment-1415541 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.