Jump to content

While/If loop misses first record - doesn't echo results


tsurko

Recommended Posts

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{ ......
                

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{ ......

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.