xtrad Posted June 5, 2010 Share Posted June 5, 2010 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. Quote Link to comment Share on other sites More sharing options...
ignace Posted June 5, 2010 Share Posted June 5, 2010 $score = ''; $position = 0; while ($row = mysql_fetch_assoc($result)) { if ($score !== $row['score']) { $score = $row['score']; ++$position; } echo $position, ' ', $score; } Quote Link to comment Share on other sites More sharing options...
xtrad Posted June 6, 2010 Author Share Posted June 6, 2010 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. Quote Link to comment Share on other sites More sharing options...
ignace Posted June 6, 2010 Share Posted June 6, 2010 $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; } Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 6, 2010 Share Posted June 6, 2010 $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 } Quote Link to comment Share on other sites More sharing options...
xtrad Posted June 6, 2010 Author Share Posted June 6, 2010 I now have the league positions displaying perfectly. Thanks for your help and quick replies. Quote Link to comment Share on other sites More sharing options...
ignace Posted June 6, 2010 Share Posted June 6, 2010 $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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.