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

Edited by nemp
Link to comment
Share on other sites

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 by AyKay47
Link to comment
Share on other sites

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>";
	?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.