drayarms Posted August 26, 2011 Share Posted August 26, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/245786-just-cant-figure-out-why-implode-doesnt-work-as-expected/ Share on other sites More sharing options...
Pikachu2000 Posted August 26, 2011 Share Posted August 26, 2011 You're trying to implode the array inside the while() loop, before it has all the values added to it. Quote Link to comment https://forums.phpfreaks.com/topic/245786-just-cant-figure-out-why-implode-doesnt-work-as-expected/#findComment-1262402 Share on other sites More sharing options...
drayarms Posted August 27, 2011 Author Share Posted August 27, 2011 @pikachu When I take the $range = implode(',' , $range); echo $range; line outside the loop as u suggested, nothing gets printed. So I guess the array is only defined within the loop. Quote Link to comment https://forums.phpfreaks.com/topic/245786-just-cant-figure-out-why-implode-doesnt-work-as-expected/#findComment-1262567 Share on other sites More sharing options...
harristweed Posted August 27, 2011 Share Posted August 27, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/245786-just-cant-figure-out-why-implode-doesnt-work-as-expected/#findComment-1262573 Share on other sites More sharing options...
drayarms Posted August 27, 2011 Author Share Posted August 27, 2011 taking both lines out of the loop makes the code work thanks all for ur contributions. Quote Link to comment https://forums.phpfreaks.com/topic/245786-just-cant-figure-out-why-implode-doesnt-work-as-expected/#findComment-1262682 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.