tsurko Posted September 8, 2013 Share Posted September 8, 2013 I'm new and I'm stuck. My code is supposed to geocode an address and then return the lat long (lat1, long1)....Then it calculates the distance between the gecoded location and some lat longs I have in my db (lat2, long2)...and then echo everything back so I can see what's being returned. Everything seems to work except the distance for the very first row in the db isn't being echoed back. Can anyone please help?? Here's the part I'm struggling with....the echo at the bottom misses the distance for the very first row although the results are returned. ....... $output= json_decode($geocode); $lat1 = $output->results[0]->geometry->location->lat; $long1 = $output->results[0]->geometry->location->lng; $jsonaddress = $output->results[0]->formatted_address; echo $jsonaddress.'<br>Lat: '.$lat1.'<br>Long: '.$long1; $raw_results = mysql_query("SELECT * FROM Artists WHERE (`First` LIKE '".$query1."%') AND (`Last` LIKE '".$query2."%')") or die(mysql_error()); function calculate_distance($long1, $lat1, $long2, $lat2) { return (3958 * 3.1415926 * sqrt(($lat2 - $lat1) * ($lat2 - $lat1) + cos($lat2 / 57.29578) * cos($lat1 / 57.29578) * ($long2 - $long1) * ($long2 - $long1)) / 180);} while($results = mysql_fetch_array($raw_results)) { $long2 = $results['Long']; $lat2 = $results['Lat']; if ( $long1 && $lat1 ) { $distance = calculate_distance($long2, $lat2, $long1, $lat1); } $distance = round($distance,0); echo "$distance Miles<br>"; echo "<p>".$results['First']." ".$results['Last']."</p>"; }} else{ ...... Quote Link to comment https://forums.phpfreaks.com/topic/281991-whileif-loop-misses-first-record-doesnt-echo-results/ Share on other sites More sharing options...
Solution tsurko Posted September 9, 2013 Author Solution Share Posted September 9, 2013 Got it myself...my numbers from my echo were running into another echo statement and getting lost. Haven't formatted anything yet and the echos are only temporary...anyway...Got it like this: ....... $output= json_decode($geocode); $lat1 = $output->results[0]->geometry->location->lat; $long1 = $output->results[0]->geometry->location->lng; $jsonaddress = $output->results[0]->formatted_address; echo $jsonaddress.'<br>Lat: '.$lat1.'<br>Long: '.$long1; $raw_results = mysql_query("SELECT * FROM Artists WHERE (`First` LIKE '".$query1."%') AND (`Last` LIKE '".$query2."%')") or die(mysql_error()); function calculate_distance($long1, $lat1, $long2, $lat2) { return (3958 * 3.1415926 * sqrt(($lat2 - $lat1) * ($lat2 - $lat1) + cos($lat2 / 57.29578) * cos($lat1 / 57.29578) * ($long2 - $long1) * ($long2 - $long1)) / 180);} while($results = mysql_fetch_array($raw_results)) { $long2 = $results['Long']; $lat2 = $results['Lat']; if ( $long1 && $lat1 ) { $distance = calculate_distance($long2, $lat2, $long1, $lat1); } $distance = round($distance,0); echo "<p>$distance Miles<p>"; echo "<p>".$results['First']." ".$results['Last']."</p>"; }} else{ ...... Quote Link to comment https://forums.phpfreaks.com/topic/281991-whileif-loop-misses-first-record-doesnt-echo-results/#findComment-1448780 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.