$result = mysql_query("SELECT * FROM quotedb WHERE moveday='$datestamp' AND driver='$driver' AND cleared='Not Cleared' AND status='Done' ORDER BY moveday, timeday") or die(mysql_error()); $distance = 0; // accumulate the distance $first_pass = true; // flag to detect the first row inside the loopwhile($row = mysql_fetch_assoc( $result )) { $lon2a=$lon2; $lat2a=$lat2; $lon1=$row['lon1']; $lat1=$row['lat1']; $lon2=$row['lon2']; $lat2=$row['lat2']; // calculate the distance from the Garage to the first point of the first row if($first_pass){ $distance += (3958*3.1415926*sqrt(($lat-$lat1)($lat-$lat1) + cos($lat/57.29578)cos($lat1/57.29578)($lon-$lon1)($lon-$lon1))/180); $first_pass = false; } // calculate the distance for each row (segment) in the route $distance += (3958*3.1415926*sqrt(($lat2-$lat1)($lat2-$lat1) + cos($lat2/57.29578)cos($lat1/57.29578)($lon2-$lon1)($lon2-$lon1))/180); if ( $lon2a == "" or $lat2a =="" ) { } else { // calculate the distance from the second point of the first row to the first point of the next row $distance += (3958*3.1415926*sqrt(($lat2a-$lat1)($lat2a-$lat1) + cos($lat2a/57.29578)cos($lat1/57.29578)($lon2a-$lon1)($lon2a-$lon1))/180); } } // calculate the distance from the second point of the last row to the Garage $distance += (3958*3.1415926*sqrt(($lat2-$lat)($lat2-$lat) + cos($lat2/57.29578)cos($lat/57.29578)($lon2-$lon)($lon2-$lon))/180);echo "$distance<br>"; ?>

Still think there is a place for improvement in the code. Will aplay Haversine method for calculations. Do you guys have some suggestion to improve this piece of code ... thx

fretburner
—
2013-05-10T12:31:23Z —
#3

Hi nitapita,

There are a couple of things that you could do which I think would improve the readability of the code and make it easier to maintain.