Jump to content

Position numbers linked to points total


deadpan

Recommended Posts

Hi,

Am relatively new to the world of php so please be gentle  ;)

 

I have a mysql table that collects a points total for each user.

What I want to do is put these user's and their scores into a high score table, with a position number depending on their score.

 

The problem I'm having, is I want the position number to be the same for users with the same score, so it would read like this:

 

Pos___Player__Points

1 ............ A ...... 200

1 ............ B ...... 200

2 ............ C ...... 180

3 ............ D ...... 150

3 ............ E ...... 150

 

Any help much appreciated!!

Link to comment
https://forums.phpfreaks.com/topic/201987-position-numbers-linked-to-points-total/
Share on other sites

Hmm... that's not how it's usually done. If you have two people tied for first, then the next person is considered to be in third place. In your example above, the person you show in 2nd place is really in third place. Also, it would have been helpful if you had provided your current code.

 

Anyway, here is how I would do it for the way you asked:

$last_points = 0;
$position = 0;
while($row = mysql_fetch_assoc($reslt))
{
    if($last_points != $row['points'])
    {
        $position++;
        $last_points = $row['points'];
    }
    echo "{$position} . . . {$row['player']} . . . {$row['points']}\n";
}

 

Or, to do it like this:

Pos___Player__Points

1 ............ A ...... 200

1 ............ B ...... 200

3 ............ C ...... 180

4 ............ D ...... 150

4 ............ E ...... 150

 

$last_points = 0;
$position = 0;
$place = 0;
while($row = mysql_fetch_assoc($reslt))
{
    $place++;
    if($last_points != $row['points'])
    {
        $position = $place;
        $last_points = $row['points'];
    }
    echo "{$position} . . . {$row['player']} . . . {$row['points']}\n";
}

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.