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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
drayarms
Forum Contributor
Posts: 134
Joined: Fri Dec 31, 2010 5:11 pm

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

Post by drayarms »

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.

Code: Select all

//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.
User avatar
angelicodin
Forum Commoner
Posts: 81
Joined: Fri Nov 13, 2009 3:17 am
Location: Oregon, USA

Re: Just can't figure out why implode doesn't work as expect

Post by angelicodin »

Not sure if I understand what your asking here, and I'm a total php noob, but It looks like it's working exactly like you are asking the code to work.

Your implode of the array $range, witch is 90011,90015,90017,90034,90037,90043,90047,90048,90056,90061,90062,90301 ,90301,90301 ,90302, will of course print out "900119001590017900349003790043900479004890056900619006290301 9030190301 90302" because it's 'removing' the "," when imploding it into a non array variable.

couple ways you can do it off the top of my head. do a for each loop and add onto the variable, ex: foreach ($range as $x){ $full_range = $full_range.",".$x; }; (I think that's right, like I said I'm not vary good at php yet), someone else help me/him if I did this wrong
genix2011
Forum Commoner
Posts: 74
Joined: Tue Aug 02, 2011 4:00 pm

Re: Just can't figure out why implode doesn't work as expect

Post by genix2011 »

Hi,

@angelicodin: no that's wrong, implode() returns an string.

@drayarms: what does print_r($range); return before the implode?
But angelicodin's thinking about the variable is not too bad, try using an different variable for the implode assignment, let's say

Code: Select all

$comma_str = implode(",", $range);
Greets.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Just can't figure out why implode doesn't work as expect

Post by twinedev »

the problem is you are doing the implode (and echo) WITHIN the while loop, so each time you it, $range is an array with a single element. When you implode that, there is only one item, so it just returns the string with no separator, and then echo it out. Then the it loops, gets the next result and does the same again. So each time in the loop you are echoing out just that result's zip code (if it is within range).

Move the implode and echo outside of the while loop and you should be good.

-Greg
drayarms
Forum Contributor
Posts: 134
Joined: Fri Dec 31, 2010 5:11 pm

Re: Just can't figure out why implode doesn't work as expect

Post by drayarms »

@angelincoding and genix, a variable can be redefined after it has already been declared and its value would be the most recent value assigned to it e.g.
$sum = 1+1 //Returns 2
$sum = $sum + 1 //Returns 3
@twinedev, 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.
genix2011
Forum Commoner
Posts: 74
Joined: Tue Aug 02, 2011 4:00 pm

Re: Just can't figure out why implode doesn't work as expect

Post by genix2011 »

Hi,

no twindev meant you should do it like that:

Code: Select all

$range = array();
while ($row2 = mysql_fetch_assoc($result2)) {
        //Define array to hold zips found within range.
        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']; }
}

$range = implode(',' , $range);
echo $range;
You are right @drayarms, variables get redefined, still it is not good practice to use the same variable for different datatypes.

Greets.
drayarms
Forum Contributor
Posts: 134
Joined: Fri Dec 31, 2010 5:11 pm

Re: Just can't figure out why implode doesn't work as expect

Post by drayarms »

taking both lines out of the loop makes the code work :) thanks all for ur contributions.
drayarms
Forum Contributor
Posts: 134
Joined: Fri Dec 31, 2010 5:11 pm

Re: Just can't figure out why implode doesn't work as expect

Post by drayarms »

taking both lines out of the loop makes the code work :) thanks all for ur contributions.
Post Reply