scrbowler Posted January 30, 2008 Share Posted January 30, 2008 I have a fantasy racing game that I have setup using php to calc weekly scores for me and it works very well. What I would like for it to do is assign the number order of finish to the results. The issue I have at hand is there are always ties in the game and I want the results to show everyone that tied for a position to have the same number of finishing position. (i.e. 3 tied for 5th place so all would end up with a 5 for their finishing order number) Has anyone dealt with this kind of scenario and if so could you offer any kind of suggestions? I you need to see the code for my scoring page I will be happy to submit it for your reference. thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/88583-solved-help-with-code-for-number-order/ Share on other sites More sharing options...
shocker-z Posted January 30, 2008 Share Posted January 30, 2008 I would personaly go about it in this way... <?php $query=mysql_query("select * from table ORDER BY score"); $i=1; while ($row=mysql_fetch_array($query)) { if ($prvscor !== $row['score']) { echo 'Position: '.$i.' '.$row['name']; } elseif ($i < 10) { $i++; echo 'Position: '.$i.' '.$row['name']; $prvscor=$row['score']; } } ?> This is untested and most likely not the fastest and neatest way of doing this but i hope it helps you. Regards Liam Quote Link to comment https://forums.phpfreaks.com/topic/88583-solved-help-with-code-for-number-order/#findComment-453546 Share on other sites More sharing options...
scrbowler Posted January 30, 2008 Author Share Posted January 30, 2008 I will test it out and report back on its success. thanks for your help Steve Quote Link to comment https://forums.phpfreaks.com/topic/88583-solved-help-with-code-for-number-order/#findComment-453550 Share on other sites More sharing options...
scrbowler Posted January 30, 2008 Author Share Posted January 30, 2008 The result posted on the page fine except that every line had a number 1 in the position column. It like it didn't see any difference in the scores. <?php include ("db.php") ; $query=mysql_query("select * from SeasonTotalsTest WHERE week='1' ORDER BY points DESC"); $i=1; while ($row=mysql_fetch_array($query)) { if ($prvscor !== $row['points']) { echo 'Position: '.$i.' '.$row['name'].'<br>' ; } elseif ($i < 10) { $i++; echo 'Position: '.$i.' '.$row['name'].'<br>' ; $prvscor=$row['points']; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88583-solved-help-with-code-for-number-order/#findComment-453611 Share on other sites More sharing options...
Psycho Posted January 30, 2008 Share Posted January 30, 2008 That's because $prvscor never getss defined, so the loop always goes through the IF portion. I would alo assume that the "position" should shift when there are ties. For example if two people tie for second, then the next person would be at position 4 (3 is skipped) <?php include ("db.php") ; $query=mysql_query("select * from SeasonTotalsTest WHERE week='1' ORDER BY points DESC"); $position_current = 0; $position_count = 0; $points_current = 0; while ($row=mysql_fetch_array($query)) { //Set position count $position_count++; if ($points_current !== $row['points']) { //New position $points_current = $row['points']; $position_current = $position_count; } //Display the current position echo "Position: $position_current {$row['name']}<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88583-solved-help-with-code-for-number-order/#findComment-453659 Share on other sites More sharing options...
scrbowler Posted January 30, 2008 Author Share Posted January 30, 2008 FANTASTIC!!!!! Thank you so much for your help. Steve Quote Link to comment https://forums.phpfreaks.com/topic/88583-solved-help-with-code-for-number-order/#findComment-453668 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.