Jump to content

Automatically numbering rows


xtrad

Recommended Posts

Hi,

 

I'm displaying an SQL table, ordered by a column named 'points'. It's showing the total points for players in a league.

 

I have used:

 

for ($n=1;$n<$nrows;$n++)

 

to number the rows of the table from 1 onwards to make it easy for players to see their position in the league. So far, so good.

 

However, as some players have the same number of points as each other, i need the PHP code to take account of this and give them the same position.  A simplified example of what i am trying to achieve is:

 

Position Points Player

1 25 A

2 23 B

3 22 C

3 22 D

5 18 E

6 15 F

 

I've searched around for advice on how to go about this but not come up with anything and would really appreciate any help.

 

Thanks.

Link to comment
Share on other sites

Excellent.  Thanks for your help.

 

To get this working i just needed to alter the line:

 

if ($score !== $row['score']) {

 

to

 

if ($score != $row['score']) {

 

Now i am wondering if there is a way to make the positioning more accurate.  For example, when two players are equal third, the positions appear so:

 

1

2

3

3

4

5

 

But really, they should appear so:

 

1

2

3

3

5

6

 

Fourth place should not exist, because equal third should be the third and fourth positions.  So, after giving equal positions, the code needs to give the next position as the row number, not simply as the next number in the sequence.

 

If there is a way to do this, i would really appreciate someone pointing me in the right direction.

 

Thanks, again.

Link to comment
Share on other sites

$score = '';
$position = 0;
$hiddenPositions = 0;
while ($row = mysql_fetch_assoc($result)) {
  if ($score != $row['score']) {
    $score = $row['score'];
    $position = $hiddenPositions + $position + 1;
    $hiddenPositions = 0;
  } else {
    ++$hiddenPositions;
  }
  echo $position, ' ', $score;
}

Link to comment
Share on other sites

$count = 1; // overall position counter
$last_score = null; // remember last score. Initialize to a value that will never exist as data
while($row = mysql_fetch_assoc($result)){ // fetch the data
if($last_score != $row['points']){
	// the points changed from the last value, set $position = $count
	$position = $count; // set the current position to be the overall position value
	$last_score = $row['points']; // remember the new score
}
echo "Position: $position, Points: {$row['points']}, Player: {$row['player']}<br />";
$count++; // increment the overall position counter
}

Link to comment
Share on other sites

$count = 1; // overall position counter
$last_score = null; // remember last score. Initialize to a value that will never exist as data
while($row = mysql_fetch_assoc($result)){ // fetch the data
if($last_score != $row['points']){
	// the points changed from the last value, set $position = $count
	$position = $count; // set the current position to be the overall position value
	$last_score = $row['points']; // remember the new score
}
echo "Position: $position, Points: {$row['points']}, Player: {$row['player']}<br />";
$count++; // increment the overall position counter
}

 

Nice, can't believe I over-complicated it so much.

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.