Jump to content

Just can't figure out why implode doesn't work as expected.


drayarms

Recommended Posts

Hello guys, I devised the code block below, to retrieve all US zip codes that are located within a specified radius from the user's own zip code.  So basically, the user selects a maximum distance option from a form element (not shown here)which is appended to the variable $distance.  What the code below does is as follows:

The longitude and latitude corresponding to the user's zip code are retrieved in the first query.  Then in the second query, the longitudes and latitudes corresponding to every zip code in the database are retrieved. In the ensuing while loop, a distance calculation is made between the user's zip and every other zip using the longitudes and latitudes and if that distance falls within the selected $distance, that particular zip code is listed in a defined array called $range. Now everything works fine up to this point as tested.  The problem is with the very last line.  I try to implode the $range array into a string with the same name, separating the array elements with commas (,).  Then when I print out the resulting string, I get the list of desired zip codes but not separated by commas. For example:

 

900119001590017900349003790043900479004890056900619006290301 9030190301 90302

 

Well when I use a foreach loop as follows:

 

foreach ($range as $r) {echo $r.",";} the $range array behaves like any normal array yielding:

 

90011,90015,90017,90034,90037,90043,90047,90048,90056,90061,90062,90301 ,90301,90301 ,90302,

 

So why in the world is the implode function not working? Here is my code.

 

//Retrieve the zip codes within range.
// Connect to the database.
require('config.php');

//Retrieve longitude and latitude of logged in member.

$query = "SELECT* FROM members INNER JOIN zip_codes ON members.zip = zip_codes.zip WHERE members.member_id = '{$_SESSION['id']}'";

$result = mysql_query($query);

$row = mysql_fetch_assoc($result);

$my_lon = $row['lon'];

$my_lat = $row['lat'];

//Query all longitudes and latitudes in database.

$query2 = "SELECT* FROM zip_codes INNER JOIN members ON members.zip = zip_codes.zip ";

$result2 = mysql_query($query2);

while ($row2 = mysql_fetch_assoc($result2)) { 

//Define array to hold zips found within range.

$range = array();  

if((rad2deg(acos(sin(deg2rad($my_lat))*sin(deg2rad($row2['lat'])) +cos (deg2rad($my_lat)) * cos (deg2rad($row2['lat'])) * cos(deg2rad($my_lon - $row2['lon']))   ) ) )*69.09 <= $distance )

 { $range[] = $row2['zip']; } 

//Implode the range arrary.

$range = implode(',' , $range);   echo $range;


}//End of while loop.

 

Link to comment
Share on other sites

while ($row2 = mysql_fetch_assoc($result2)) { 

//Define array to hold zips found within range.

$range = array();  

if((rad2deg(acos(sin(deg2rad($my_lat))*sin(deg2rad($row2['lat'])) +cos (deg2rad($my_lat)) * cos (deg2rad($row2['lat'])) * cos(deg2rad($my_lon - $row2['lon']))   ) ) )*69.09 <= $distance )

 { $range[] = $row2['zip']; } 

//Implode the range arrary.

$range = implode(',' , $range);   echo $range;


}//End of while loop.

 

The array is redifined every time the loop loops! so will at best only contain one zip code....

 

//Define array to hold zips found within range.

$range = array();  

while ($row2 = mysql_fetch_assoc($result2)) { 



if((rad2deg(acos(sin(deg2rad($my_lat))*sin(deg2rad($row2['lat'])) +cos (deg2rad($my_lat)) * cos (deg2rad($row2['lat'])) * cos(deg2rad($my_lon - $row2['lon']))   ) ) )*69.09 <= $distance )

 { $range[] = $row2['zip']; } 




}//End of while loop.

//Implode the range arrary.

$range = implode(',' , $range);   echo $range;

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.