tags:

views:

147

answers:

5

Hi everyone,

In my CakePHP app, I'm pulling in the results of a table called 'Timesheets'. This table has an athlete_id and a few split times for a race. I need to do some calculations on one athlete's times compared to another athlete (for instance, calculate the difference between the fastest finish time and slowest).

I thought the best way to do this would be to store every athletes 'finish' in a new array, then run the min() function on it. For the life of me, I can't get this to work...

Do you think I am approaching this the wrong way, or am I just missing something?

foreach ($timesheet['Run'] as $run):
 <tr<?php echo $class;?>>
  <td><?php echo $run['athlete_id'];?></td>
  <td><?php echo $run['start'];?></td>
  <td><?php echo $run['split1'];?></td>
  <td><?php echo $run['split2'];?></td>
  <td><?php echo $run['split3'];?></td>
  <td><?php echo $run['split4'];?></td>
  <td><?php echo $run['split5'];?></td>
  <td><?php echo $run['split6'];?></td>
  <td><?php echo $run['finish'];?></td>
    </tr>
   endforeach
A: 

I'll assume you have the appropriate tags around the foreach and a ';' after endforeach.

So the question becomes can you access $timesheet['Run'][0]['athlete_id']? You might use print_r() to make sure that $timesheet is in the form you think it is.

Myles
A: 

Hi kyle, I would look into using something like usort. You can define a callback function to sort your $timesheet['Run'] array by the 'finish' key. In the end, the first entry in your array, if defined correctly, will have the shortest finish time.

For example:

function cmp($a, $b)
{
   if ($a['finish'] == $b['finish'])
   { 
      return 0;
   }

   return ($a['finish'] < $b['finish']) ? -1 : 1;
}

usort($timesheet['Run'], "cmp");
Matt Caldwell
A: 

I recommend either doing the calculation and sorting in the controller and in SQL.

David W
+1  A: 

Assuming you are fetching the timetable with order by finish asc from the database, you could do something like this (omitting splits for brevity):

$fastest = $previous = $timesheet['Run'][0]['finish'];
foreach ($timesheet['Run'] as $run):
    <tr>
        <td><?php echo $run['athlete_id'];?></td>
        <td><?php echo $run['finish'];?></td>
        <td><?php echo $previous - $run['finish'];?></td>
        <td><?php echo $fastest - $run['finish'];?></td>
    </tr>
    $previous = $run['finish'];
endforeach

This should give rows like this:

id | finished_in | to_previous  | to_best
1  | 120         | 0            | 0
2  | 121.5       | -1.5         | -1.5
3  | 121.8       | -0.3         | -1.8
4  | 122.6       | -0.8         | -2.6
...

Another option would be to wrap the returned array into an Object or maybe an ArrayIterator and do the calulcations inside, encapsuling $fastest and $previous and whatever else you might want to calculate.

Gordon
A: 

In my opinion you need to change the way you store your data. try to create table containing id, event_id, athlete_id and time, that would make your application more flexible and calculation easier.

Nazariy