Matthew Barraud
Matthew Barraud

Reputation: 515

Go back in time on a foreach loop

I have a foreach loop running through database items; The loop is outputting table rows with waypoint positions.

I am using some functions to work out the bearing and distances to the next waypoint.

  $startingLat = $passagePlans->first()->latitude;
  startingLong = $passagePlans->first()->longitude;

  foreach ($passagePlans->results() as $passageplan) {

  if ($i >= 1) {
  // Set the new lat/lon coordinates
  $newLatitude = $passageplan->latitude;
  $newLongitude = $passageplan->longitude;
  // Set the bearing and distance variables
  $bearing = bearing($startingLat, $startingLong, $newLatitude, $newLongitude);
  $distance = distance($startingLat, $startingLong, $newLatitude, $newLongitude);
  // Update the new starting lat/long with the last waypoint ready for the next iteration through the GPX file
  $startingLat = $newLatitude;
  $startingLong = $newLongitude;
  }
  $i++;

  echo '
  <tr>
  <td rowspan="2">'.$passageplan->waypoint_number.'</td>
  <td>'.$passageplan->waypoint_name.'</td>
                    <td>'.convertDecimalToDMSLatitude($passageplan->latitude).'</br>'.convertDecimalToDMSLongitude($passageplan->longitude).'</td>
  <td>'.$bearing.'°</td>
  <td>'.$distance.'</td>
  <td>'.$passageplan->vhf.'</td>
  <td>HW '.$passageplan->hw_height.'<sup>m</sup> at '.$passageplan->hw_time.' </br>LW '.$passageplan->lw_height.'<sup>m</sup> at '.$passageplan->lw_time.'</td>
  <td>Charts: '.$passageplan->chart_num.'<br />Almanac: '.$passageplan->almanac.'<br />Radio Signals: '.$passageplan->radio_signals.'</td>
  <td>'.$passageplan->por.'<br />Almanac: '.$passageplan->por_almanac.'<br />VHF CH: '.$passageplan->por_vhf.'</td>
  <td align="right" >
  <a href="/pages/voyages/passageplans/editwaypoint.php?voyage='.Input::get('voyage').'&plan='.Input::get('plan').'&waypoint='.$passageplan->waypoint_id.'" class="btn btn-sm btn-outline-primary">edit</a>
  <a href="/pages/voyages/passageplans/deletewaypoint.php?voyage='.Input::get('voyage').'&plan='.Input::get('plan').'&waypoint='.$passageplan->waypoint_id.'" class="btn btn-sm btn-outline-danger">delete</a>
  </td>
  </tr>
  <tr>
  <td colspan="9">
  '.$passageplan->passage_note.'
  </td>
  </tr>
  ';
  }
  ?>

My problem is I need both the starting lat/long and the next records lat/long to work out the bearing and distance.

The foreach loop is messing me up as I can only see the current lat/long or row we are parsing through.

So table row 1 shows the information but no bearing and distance. And row 2 shows the bearing and distance that should be in row 1 because it needs the new lat/long to process the information.

Is there any way to look ahead at the next entry in a foreach loop prior to actually iterating through to it??

Can anyone see a better way of doing this?

Could I use the next function? https://www.php.net/manual/en/function.next.php

Would

$bearing = bearing($startingLat, $startingLong, next($passageplan->$newLatitude), $passageplan->newLongitude);

work??

Upvotes: 0

Views: 177

Answers (2)

Igor Еfron
Igor Еfron

Reputation: 26

First, prepare an Array, where each entry has all the needed Data.
Then loop through the perpared Array

$passagePlansPrepared = array();
$i = 0;
foreach ($passagePlans->results() as $passageplan) {
    $passagePlansPrepared[$i]['plan'] = $passageplan;
    $passagePlansPrepared[$i]['latitude'] = $passageplan->latitude;
    $passagePlansPrepared[$i]['longitude'] = $passageplan->longitude;
    if ($i > 0) {
        $passagePlansPrepared[$i - 1]['latitude_2'] = $passageplan->latitude;
        $passagePlansPrepared[$i - 1]['longitude_2'] = $passageplan->longitude;  
    }
    $i++;
}

foreach ($passagePlansPrepared as $passageplan) {
    if (!isset[$passageplan['latitude_2']]) {
        continue; // skip last row
    }
    $bearing = bearing($passageplan['latitude'], $passageplan['longitude'], $passageplan['latitude_2'], $passageplan['longitude_2']);
    ...
    $passageplan['plan']->vhf
    ...              
}

Upvotes: 1

Genhis
Genhis

Reputation: 1524

Assuming that $passagePlans->results() returns a numbered array, you can use regular for loop.

$results = $passagePlans->results();

// You can't access next element when the current is the last one, so subtract -1
// to prevent going out of bounds
$end = count($results) - 1;
for($i=0; $i < $end; $i++) {
    $first = $results[0];
    $current = $results[$i];
    $next = $results[$i + 1];
}

Upvotes: 0

Related Questions