i have a application in which i want to get the zipcode or search based on the zipcode.when the users give the radius and a particular zipcode..i want to get the zip code based onthe radius .please tell me how to do that..

Recommended Answers

All 11 Replies

why do you need to get the zip code when they are giving it to you?

why do you need to get the zip code when they are giving it to you?

sorry.depends on the radius we want to find zipcode and areas.

i have a db which contains the logitude latitudes,city and zipcode.when the user gives specifies a zipcode and search 30 radius from that zipcode,i want to display all the places inside that zipcode

you have to use a formula to calculate the distance based on latitude and longitude. Once you have the distance you can use it to return all of the zipcodes within that radius. You should be able to find it online, otherwise I will post the code when I am in front of my home computer.

thanks for your reply. i have formula to calculate the radius..but i dont have any idea how to get the zipcode based on the radius

you have to loop through the zipcodes and calculate their distance from the current location and see if they are below the specified radius.

based on the zipcode how to find the radius.is there any formula

i am confused. why do you need to find a radius base on the zipcode. maybe if you explained what you are trying to do in depth i could follow what you are doing.

when the user gives a zip code and specifies the radius i should get all the place inside that radius.
my table consists of
CREATE TABLE `zipcodes` (
`zip` int(5) NOT NULL,
`ziptype` char(1) default NULL,
`city` varchar(45) NOT NULL,
`citytype` varchar(45) NOT NULL,
`stateName` varchar(45) NOT NULL,
`state` char(2) NOT NULL,
`area_code` char(3) NOT NULL,
`latitude` float NOT NULL default '0',
`longitude` float NOT NULL default '0'
based on this i have to calculate

try this. has worked for me numerous times.


//make sure you add your database connection here

function getCoords( $zip ) {
	$sql   = "SELECT `latitude`,`longitude` FROM `zipcodes` WHERE `zip` = " . mysql_real_escape_string( $zip );
	$query = mysql_query( $sql );
	$res   = mysql_fetch_row( $query );
	$data['lat']  = $res[0];
	$data['long'] = $res[1];
	return $data;

$zcode = 65456; //this can come from a form. doesn't matter.
$radius = 20; //this can come from a form as well. this is just an example.

$coords = getCoords( $zcode );
$latitude = $coords['lat'] == '' ? 0 : $coords['lat'];
$longitude = $coords['long'] == '' ? 0 : $coords['long'];
$sql   = "SELECT *,(((acos(sin((" . $latitude . "*pi()/180)) * sin((`latitude`*pi()/180))+cos((" . $latitude . "*pi()/180)) * cos((`latitude`*pi()/180)) * cos(((" . $longitude . "- `longitude`)*pi()/180))))*180/pi())*60*1.1515) as `distance` FROM `zipcodes` GROUP BY `zip` HAVING distance <= " . $radius;
$query = mysql_query($sql);
$total = mysql_num_rows($query);
if ( $total > 0 ) {
	while( $row = mysql_fetch_assoc( $query ) ) {
		echo "{$row['city']}, {$row['state']} - {$row['zip']}<br />";
else {
	echo 'no results found!';


thanks for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.