Jump to content

[SOLVED] Help with code for number order


scrbowler

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/88583-solved-help-with-code-for-number-order/
Share on other sites

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

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'];
  }
}
?>

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

}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.