Jump to content


Photo

Sort Teams By Record and Tie Breakers


  • Please log in to reply
7 replies to this topic

#1 Mateobus

Mateobus
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 08 August 2006 - 08:58 PM

Ok I have teams in arrays and i want to sort them to make a leaderboard.  Here are the array values:

$team['index'] //starting with 1
$team['name']
$team['conference_points']
$team['goal_differential']
$team['goals_for']
$team['goals_against']
$team['name']

The rules for rankings are as follows.  Team with most points is first. If points are the same, the tiebreaker is whoever has the greatest goal differential. If goal diff is the same, its team with the most goals for. How could I do this with PHP?  Thanks for all the help.

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 08 August 2006 - 09:06 PM

array_multi_sort(
$team['conference_points'],SORT_NUMERIC, SORT_DESC,
$team['goal_differential'],SORT_NUMERIC, SORT_DESC,
$team['goals_for'],SORT_NUMERIC, SORT_DESC,
$team['goals_against'],SORT_NUMERIC, SORT_ASC,
$team['name'],SORT_STRING, SORT_ASC,
$team['index'],SORT_NUMERIC, SORT_ASC
);

I include all teh fields in this multisort as I have tried in the past to just sort on the sub-arrays required but then indices got lost......
follow me on twitter @PHPsycho

#3 Mateobus

Mateobus
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 08 August 2006 - 09:14 PM

Now I have confused myself.  Should it really be like
$team1['index'] //starting with 1
$team1['name']
$team1['conference_points']
$team1['goal_differential']
$team1['goals_for']
$team1['goals_against']
$team1['name']

$team2['index'] //starting with 1
$team2['name']
$team2['conference_points']
$team2['goal_differential']
$team2['goals_for']
$team2['goals_against']
$team2['name']
.
.
.
Or should i use matrices:
$team['index']['name'] //starting with 1
$team['index']['conference_points']
$team['index']['goal_differential']
$team['index']['goals_for']
$team['index']['goals_against']
$team['index']['name']

Which of these should i do.  I am leaning towards the latter, but how would i print them in a table with echo statements using your array_multi_sort... Thanks again ahead of time



#4 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 08 August 2006 - 09:25 PM

no what you had initiall is fine... and the multisort will work fine on that
to put it all in a table....

<table>
 <thead>
  <tr>
   <th>Team</th>
   <th>GF</th>
   <th>GA</th>
   <th>GD</th>
   <th>Points</th>
  </tr>
 </thead>
 <tbody>
<?php
$i = 0;
$stop = count($team['index']);
while ($i < $stop)
{
?>
  <tr>
   <td><?php echo $team['name'][$i]; ?></td>
   <td><?php echo $team['goals_for'][$i]; ?></td>
   <td><?php echo $team['goals_against'][$i]; ?></td>
   <td><?php echo $team['goal_difference'][$i]; ?></td>
   <td><?php echo $team['conference_points'][$i]; ?></td>
  </tr>
<?php
 $i++;
}

?>
 </tbody>
</table>


follow me on twitter @PHPsycho

#5 Mateobus

Mateobus
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 08 August 2006 - 09:38 PM

Now i got a call to undefined function array_mult_sort error.  Is it my version of php or something?

#6 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 08 August 2006 - 09:46 PM

no its my bad...

array_multisort
follow me on twitter @PHPsycho

#7 Mateobus

Mateobus
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 08 August 2006 - 10:00 PM

Ok, here is the problem, how do i initially put all of the values into arrays, if i don't have a second index array.  For example

for ($i=1; $i<7; $i++){
$team['wins'] = team wins value

}
i will keep overwritng the team wins amount.  these should be different for different teams.  i need to use matrices to store the index value dont i?

#8 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 08 August 2006 - 10:25 PM

yes you do.
follow me on twitter @PHPsycho




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users