ranking based on calculations

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
esiason14
Forum Newbie
Posts: 10
Joined: Sun Jul 17, 2005 12:19 pm

ranking based on calculations

Post by esiason14 »

Hey all,
I'm trying to rank football players based on some calculations ($row['ffr_points']). I've got it working, but with one small problem...it doesn't take into account those players with the same point totals. For those players that have the same pt total, I would like to assign them the same rank #. Some pointers to get me in the right direction would be appreciated.

This is what I have so far:

Code: Select all

while ($row = mysql_fetch_array($result))
{
  $row['ffr_points'] = (($row['sumpassyd'] * 0.05) + ($row['sumpasstd'] * 4) + ($row['sumqbint'] * -2) + ($row['sumrecyd'] * 0.1) +  ($row['sumrushyd'] * 0.1) +  ($row['sumrecatt'] * 1)  + ($row['sumtotaltd'] * 6));
  $rows[] = $row;
}

usort($rows, 'sort_rows_by_points');

$playerRank = 0;
echo "<table align=\"center\">";

foreach($rows as $row)
{
  $playerRank++;
  
   print "<tr><td>$playerRank. <img src=\"../images/NFL/logos/$row[nflteam_icon]\"> $row[fname] $row[lname]</td></tr>";
   
}

echo "</table>";

function sort_rows_by_points($a, $b)
{
  if($a['ffr_points'] < $b['ffr_points']) return 1;
  if($a['ffr_points'] > $b['ffr_points']) return -1;
  return 0;
} 
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What do you mean by "assign them the same rank"? The code is doing that.
esiason14
Forum Newbie
Posts: 10
Joined: Sun Jul 17, 2005 12:19 pm

Post by esiason14 »

feyd wrote:What do you mean by "assign them the same rank"? The code is doing that.
For example, these players have the same point total:

Code: Select all

62. Kevin Johnson with 30.3 points
63. Wayne Chrebet with 30.3 points
....but they are assigned different ranks (#62 and #63). I would like it to assign them the same rank (ie #62 as above) if they have the same point total
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Add some analysis logic to your foreach. Compare the calculation of the previous iteration to the current iteration.
esiason14
Forum Newbie
Posts: 10
Joined: Sun Jul 17, 2005 12:19 pm

Post by esiason14 »

Ok, let me give it a go and see if I can get it.
Post Reply