I’ve got a foreach loop in PHP that chucks out numbers from a GPX file and produces a table.
For example:
Distance | Elevation
--------------------
0 | 268
0.27 | 294
0.87 | 294
1.14 | 321
1.25 | 324
1.49 | 371
2.30 | 399
3.00 | 372
3.91 | 346
I want to add up when the elevation gets higher. So, for this table:
Increase: 294 – 268 = 26, 294 – 294 = 0 (26), 321 – 294 = 27 (53), 324 – 321 = 3 (56)….. and so on
I also want to count when it decreases and keep it separate to get total increase and total decrease in elevation.
So, I’m basically wanting to ask if each previous number is higher or lower and then, depending on that, add it to a variable for use later.
Any ideas?
I tried to keep it simple but code was asked for… apologies in advance!
<?php
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
$arrData = array();
// if input is object, convert into array
if (is_object($arrObjData)) {
$arrObjData = get_object_vars($arrObjData);
}
if (is_array($arrObjData)) {
foreach ($arrObjData as $index => $value) {
if (is_object($value) || is_array($value)) {
$value = objectsIntoArray($value, $arrSkipIndices); // recursive call
}
if (in_array($index, $arrSkipIndices)) {
continue;
}
$arrData[$index] = $value;
}
}
return $arrData;
}
//$xmlUrl = "../wp-content/uploads/2012/11/stoodley-pike-walk-cragg-vale-withens-clough-circular.gpx"; // XML feed file/URL
$uploadyr = $_GET['yr'];
$uploadmth = $_GET['mth'];
$gpx = $_GET['gpx'];
$xmlUrl = "../wp-content/uploads/" . $uploadyr . "/" . $uploadmth . "/" . $gpx . ".gpx";
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr);
$arrXml = objectsIntoArray($xmlObj);
?>
<!doctype html>
<head>
<title>Graph</title>
<!--<link href="visualize.css" rel="stylesheet">
<link href="visualize-light.css" rel="stylesheet">-->
<link href="visualize-override.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="visualize.jQuery.js"></script>
<script>
$(document).ready(function(){
$('#elevation').visualize({
type: 'area',
parseDirection: 'y',
colors: ['#339933','#00ff00','#0000ff','#ffff00','#ff00ff','#00ffff','#000000','#666666','#AAAAAA']
});
$('.visualize-labels-x li:odd').hide();
});
</script>
</head>
<body>
<div id="wrapper">
<?php
echo '<table id="elevation"><caption>Elevation Profile</caption><thead><td>Total Distance (mi)</td><th>Elevation</th></thead><tbody>';
foreach($arrXml["rte"]["rtept"] as $single){
$lat1 = $prevLat;
$lng1 = $prevLon;
if($prevLat == null && $prevLon == null){
$lat1 = $single["@attributes"]["lat"];
$lng1 = $single["@attributes"]["lon"];
}
$lat2 = $single["@attributes"]["lat"];
$lng2 = $single["@attributes"]["lon"];
$pi80 = M_PI / 180;
$lat1 *= $pi80;
$lng1 *= $pi80;
$lat2 *= $pi80;
$lng2 *= $pi80;
$r = 6372.797; // mean radius of Earth in km
$dlat = $lat2 - $lat1;
$dlng = $lng2 - $lng1;
$a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2);
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
$km = $r * $c;
$distance += $km;
$buildrow = '';
$buildrow .= '<tr>';
if($prevLat !== null && $prevLon !== null){
$buildrow .= '<th>' . number_format(($miles ? ($distance * 0.621371192) : $distance),2) . '</th>';
} else {
$buildrow .= '<th>0</th>';
}
$buildrow .= '<td>' . number_format($single["ele"],0) . '</td>';
$buildrow .= '</tr>';
echo $buildrow;
$prevLat = $single["@attributes"]["lat"];
$prevLon = $single["@attributes"]["lon"];
}
echo '</tbody></table>';
echo '<p>Total Distance: ' . number_format($distance,2) . 'mi</p>';
?>
</div>
</body>
</html>
You just simply need to add a variable before the loop to hold the previous value. Say
$prevValue. Then in the loop, at the end of the loop, you set it to the current value. When the loop runs the next one, it’ll be the previous value.Then, just check if it’s more or less then the current value.