Jump to content

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


tsurko
Go to solution Solved by 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{ ......
                
Link to comment
Share on other sites

  • Solution

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{ ......
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.