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

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.