deadpan Posted May 16, 2010 Share Posted May 16, 2010 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!! Quote Link to comment https://forums.phpfreaks.com/topic/201987-position-numbers-linked-to-points-total/ Share on other sites More sharing options...
Psycho Posted May 17, 2010 Share Posted May 17, 2010 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/201987-position-numbers-linked-to-points-total/#findComment-1059269 Share on other sites More sharing options...
deadpan Posted May 18, 2010 Author Share Posted May 18, 2010 Works perfectly - and yep your absolutely correct in how the position number should descend Thanks mjdamato. Quote Link to comment https://forums.phpfreaks.com/topic/201987-position-numbers-linked-to-points-total/#findComment-1059819 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.