8 Replies - 762 Views - Last Post: 27 August 2011 - 01:45 PM Rate Topic: -----

#1 drayarms  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 18-May 11

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

Posted 26 August 2011 - 12:52 PM

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.



Is This A Good Question/Topic? 0
  • +

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

#2 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

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

Posted 26 August 2011 - 01:09 PM

So what is displayed when you echo out the $range?
Was This Post Helpful? 0
  • +
  • -

#3 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

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

Posted 26 August 2011 - 01:30 PM

It's not imploding anything because there's nothing to implode. You're creating an array of 1 item every single time, and echoing it out, which means you'll get the same output.

You need to do this:
// Define array to hold zips found within range.
$range = array();

while ($row2 = mysql_fetch_assoc($result2)) { 
	if( /* Whatever */ )
		$range[] = $row2['zip'];
} // End of while loop.

// Implode the range arrary.
$range = implode(',', $range);
echo $range;

Was This Post Helpful? 0
  • +
  • -

#4 drayarms  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 18-May 11

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

Posted 27 August 2011 - 01:17 AM

@Rudivisser

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. :(
Was This Post Helpful? 0
  • +
  • -

#5 E_Geek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 45
  • View blog
  • Posts: 236
  • Joined: 20-February 11

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

Posted 27 August 2011 - 02:12 AM

Rudivisser's code works fine as he has it, granted $range is given values.

Show us your implementation ?
Was This Post Helpful? 0
  • +
  • -

#6 Ace26  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 183
  • Joined: 10-August 08

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

Posted 27 August 2011 - 04:42 AM

View Postdrayarms, on 27 August 2011 - 10:17 AM, said:

@Rudivisser

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. :(


Did you also remove
$range = array();
from within the while loop like in the code given to you? That might just be it.

This post has been edited by Ace26: 27 August 2011 - 04:43 AM

Was This Post Helpful? 2
  • +
  • -

#7 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

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

Posted 27 August 2011 - 04:56 AM

View PostAce26, on 27 August 2011 - 12:42 PM, said:

Did you also remove
$range = array();
from within the while loop like in the code given to you? That might just be it.

Exactly what I was thinking; that was the primary problem.

See what you was doing was creating an empty array (ie. resetting $range) on every single iteration, which is why nothing was being implode()d in the first place.
Was This Post Helpful? 2
  • +
  • -

#8 drayarms  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 18-May 11

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

Posted 27 August 2011 - 01:36 PM

@Rudviser, Well when I first formulated this code on a single standalone page, I had the $range = array() line outside the while loop. Then when I made it part of a larger page, strangely enough PHP didn't recognize $range to be an array. On the line where I append the array values within the while loop (ie the $range = $row2['zip'] line), I got an error message which said something like, "the [] operator doesn't work for strings" (i'm paraphrasing). That's why I moved it into the loop in the first place. But odly enough, As a stand alone page, it works just fine with $range = array() being placed outside the loop. I'm really confused.
Was This Post Helpful? 0
  • +
  • -

#9 drayarms  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 18-May 11

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

Posted 27 August 2011 - 01:45 PM

@rudviser, well it works now, after i took both lines outside the loop. thank you all for your contributions :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1