Jump to content

Need help with a table in PHP - sorting by ID


nemp

Recommended Posts

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:

 

 

 

r9r8f8.png

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`.

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>";
	?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.