PHP/MySQL Zip Shop Finder
- AmieCutie
- Newbie


- Joined: Aug 17, 2011
- Posts: 12
- Status: Offline
So I've been working on a page to locate shops based on zip code and mile distance. I used this tutorial as a walk-through http://htmlcampusDOTcom/build-a-zip-cod ... on-in-php/
Now I've changed a couple of things like the mySQL table is called locations instead of zip_codes. I've tested it and it connects to the database and all, but it just says that no results were found, even when I enter in an exact zip code of one of the shops (90278 or 92602). http://bitDOTly/Me2d1M
Below is my code for the page (password left out). I hope someone could spot what might be wrong, something I missed.
Now I've changed a couple of things like the mySQL table is called locations instead of zip_codes. I've tested it and it connects to the database and all, but it just says that no results were found, even when I enter in an exact zip code of one of the shops (90278 or 92602). http://bitDOTly/Me2d1M
Below is my code for the page (password left out). I hope someone could spot what might be wrong, something I missed.
PHP Code: [ Select ]
<?php
// Create page variables
$r = NULL;
$z = NULL;
$shops = NULL;
$Errors = NULL;
// Establish DB connection
mysql_connect ('localhost', 'acq-admin', 'password') or die(mysql_error());
mysql_select_db ('autocareq') or die(mysql_error());
// Declare page functions
function Dist ($lat_A, $long_A, $lat_B, $long_B) {
$distance = sin(deg2rad($lat_A))
* sin(deg2rad($lat_B))
+ cos(deg2rad($lat_A))
* cos(deg2rad($lat_B))
* cos(deg2rad($long_A - $long_B));
$distance = (rad2deg(acos($distance))) * 69.09;
return $distance;
}
### Handle form if submitted
if (isset ($_POST['submitted'])) {
// Validate Zip code field
if (!empty ($_POST['zip']) && is_numeric ($_POST['zip'])) {
$z = (int)$_POST['zip'];
// Verify zip code exists
$query = "SELECT lat, lon FROM locations WHERE zip = '$z'";
$result = mysql_query ($query);
if (mysql_num_rows ($result) == 1) {
$zip = mysql_fetch_assoc ($result);
} else {
$Errors = '<p>The zip code you entered was not found!</p>';
}
}
// Validate radius field
if (isset ($_POST['radius']) && is_numeric ($_POST['radius'])) {
$r = (int)$_POST['radius'];
}
// Proceed if no errors were found
if ($r && $z) {
// Retrieve coordinates of the shops
$shops = array();
$query = "SELECT name, address, city, state, zip, phone, lat, lon
FROM dealerships
INNER JOIN locations
ON dealerships.zip = locations.zip";
$result = mysql_query ($query);
// Go through and check all shops
while ($row = mysql_fetch_assoc ($result)) {
// Separate closest shops
$distance = Dist ($row['lat'], $row['lon'], $zip['lat'], $zip['lon']);
// Check if shop is in radius
if ($distance <= $r) {
$shops[] = array (
'name' => $row['name'],
'address' => $row['address'],
'state' => $row['state'],
'city' => $row['city'],
'zip' => $row['zip'],
'phone' => $row['phone']
);
}
}
} else {
$Errors = ($Errors) ? $Errors : '<p>Errors were found please try again!</p>';
}
}
?><html>
<head>
<title>Pick Your Shop</title>
</head>
<body>
<form action="" method="post">
<p>Enter your zip code below to find the nearest shop</p>
<?php echo ($Errors) ? $Errors : ''; ?>
<div>
<label>Zip:</label>
<input name="zip" type="text" size="10" maxlength="5" />
</div>
<div>
<label>Search Area:</label>
<select name="radius" id="radius">
<option value="5">5 mi.</option>
<option value="10">10 mi.</option>
<option value="15">15 mi.</option>
<option value="20">20 mi.</option>
</select>
</div>
<div>
<input type="hidden" name="submitted" value="submitted" />
<input type="submit" value="Submit" />
</div>
</form>
<?php
if (isset ($shops)) {
if (!empty ($shops)) {
echo '<p><strong>' . count ($shops) . ' results were found.</strong></p>';
foreach ($shops as $value) {
echo '<p><strong>' . $value['name'] . '</strong><br />';
echo $value['address'] . '<br />';
echo $value['city'] . ', ' . $value['state'] . ' ' . $value['zip'];
echo ' <a target="_blank" href="http://maps. google. com/maps?q=',
$value['address'], ' ',
$value['city'], ', ',
$value['state'], ' ',
$value['zip'],
'">Map this location</a><br />';
echo 'Phone: ' . $value['phone'];
echo '</p>';
}
} else {
echo '<p><strong>No results found</strong></p>';
echo '<p><strong>' . $z . '</strong><br />';
}
}
?>
</body>
</html>
// Create page variables
$r = NULL;
$z = NULL;
$shops = NULL;
$Errors = NULL;
// Establish DB connection
mysql_connect ('localhost', 'acq-admin', 'password') or die(mysql_error());
mysql_select_db ('autocareq') or die(mysql_error());
// Declare page functions
function Dist ($lat_A, $long_A, $lat_B, $long_B) {
$distance = sin(deg2rad($lat_A))
* sin(deg2rad($lat_B))
+ cos(deg2rad($lat_A))
* cos(deg2rad($lat_B))
* cos(deg2rad($long_A - $long_B));
$distance = (rad2deg(acos($distance))) * 69.09;
return $distance;
}
### Handle form if submitted
if (isset ($_POST['submitted'])) {
// Validate Zip code field
if (!empty ($_POST['zip']) && is_numeric ($_POST['zip'])) {
$z = (int)$_POST['zip'];
// Verify zip code exists
$query = "SELECT lat, lon FROM locations WHERE zip = '$z'";
$result = mysql_query ($query);
if (mysql_num_rows ($result) == 1) {
$zip = mysql_fetch_assoc ($result);
} else {
$Errors = '<p>The zip code you entered was not found!</p>';
}
}
// Validate radius field
if (isset ($_POST['radius']) && is_numeric ($_POST['radius'])) {
$r = (int)$_POST['radius'];
}
// Proceed if no errors were found
if ($r && $z) {
// Retrieve coordinates of the shops
$shops = array();
$query = "SELECT name, address, city, state, zip, phone, lat, lon
FROM dealerships
INNER JOIN locations
ON dealerships.zip = locations.zip";
$result = mysql_query ($query);
// Go through and check all shops
while ($row = mysql_fetch_assoc ($result)) {
// Separate closest shops
$distance = Dist ($row['lat'], $row['lon'], $zip['lat'], $zip['lon']);
// Check if shop is in radius
if ($distance <= $r) {
$shops[] = array (
'name' => $row['name'],
'address' => $row['address'],
'state' => $row['state'],
'city' => $row['city'],
'zip' => $row['zip'],
'phone' => $row['phone']
);
}
}
} else {
$Errors = ($Errors) ? $Errors : '<p>Errors were found please try again!</p>';
}
}
?><html>
<head>
<title>Pick Your Shop</title>
</head>
<body>
<form action="" method="post">
<p>Enter your zip code below to find the nearest shop</p>
<?php echo ($Errors) ? $Errors : ''; ?>
<div>
<label>Zip:</label>
<input name="zip" type="text" size="10" maxlength="5" />
</div>
<div>
<label>Search Area:</label>
<select name="radius" id="radius">
<option value="5">5 mi.</option>
<option value="10">10 mi.</option>
<option value="15">15 mi.</option>
<option value="20">20 mi.</option>
</select>
</div>
<div>
<input type="hidden" name="submitted" value="submitted" />
<input type="submit" value="Submit" />
</div>
</form>
<?php
if (isset ($shops)) {
if (!empty ($shops)) {
echo '<p><strong>' . count ($shops) . ' results were found.</strong></p>';
foreach ($shops as $value) {
echo '<p><strong>' . $value['name'] . '</strong><br />';
echo $value['address'] . '<br />';
echo $value['city'] . ', ' . $value['state'] . ' ' . $value['zip'];
echo ' <a target="_blank" href="http://maps. google. com/maps?q=',
$value['address'], ' ',
$value['city'], ', ',
$value['state'], ' ',
$value['zip'],
'">Map this location</a><br />';
echo 'Phone: ' . $value['phone'];
echo '</p>';
}
} else {
echo '<p><strong>No results found</strong></p>';
echo '<p><strong>' . $z . '</strong><br />';
}
}
?>
</body>
</html>
- <?php
- // Create page variables
- $r = NULL;
- $z = NULL;
- $shops = NULL;
- $Errors = NULL;
- // Establish DB connection
- mysql_connect ('localhost', 'acq-admin', 'password') or die(mysql_error());
- mysql_select_db ('autocareq') or die(mysql_error());
- // Declare page functions
- function Dist ($lat_A, $long_A, $lat_B, $long_B) {
- $distance = sin(deg2rad($lat_A))
- * sin(deg2rad($lat_B))
- + cos(deg2rad($lat_A))
- * cos(deg2rad($lat_B))
- * cos(deg2rad($long_A - $long_B));
- $distance = (rad2deg(acos($distance))) * 69.09;
- return $distance;
- }
- ### Handle form if submitted
- if (isset ($_POST['submitted'])) {
- // Validate Zip code field
- if (!empty ($_POST['zip']) && is_numeric ($_POST['zip'])) {
- $z = (int)$_POST['zip'];
- // Verify zip code exists
- $query = "SELECT lat, lon FROM locations WHERE zip = '$z'";
- $result = mysql_query ($query);
- if (mysql_num_rows ($result) == 1) {
- $zip = mysql_fetch_assoc ($result);
- } else {
- $Errors = '<p>The zip code you entered was not found!</p>';
- }
- }
- // Validate radius field
- if (isset ($_POST['radius']) && is_numeric ($_POST['radius'])) {
- $r = (int)$_POST['radius'];
- }
- // Proceed if no errors were found
- if ($r && $z) {
- // Retrieve coordinates of the shops
- $shops = array();
- $query = "SELECT name, address, city, state, zip, phone, lat, lon
- FROM dealerships
- INNER JOIN locations
- ON dealerships.zip = locations.zip";
- $result = mysql_query ($query);
- // Go through and check all shops
- while ($row = mysql_fetch_assoc ($result)) {
- // Separate closest shops
- $distance = Dist ($row['lat'], $row['lon'], $zip['lat'], $zip['lon']);
- // Check if shop is in radius
- if ($distance <= $r) {
- $shops[] = array (
- 'name' => $row['name'],
- 'address' => $row['address'],
- 'state' => $row['state'],
- 'city' => $row['city'],
- 'zip' => $row['zip'],
- 'phone' => $row['phone']
- );
- }
- }
- } else {
- $Errors = ($Errors) ? $Errors : '<p>Errors were found please try again!</p>';
- }
- }
- ?><html>
- <head>
- <title>Pick Your Shop</title>
- </head>
- <body>
- <form action="" method="post">
- <p>Enter your zip code below to find the nearest shop</p>
- <?php echo ($Errors) ? $Errors : ''; ?>
- <div>
- <label>Zip:</label>
- <input name="zip" type="text" size="10" maxlength="5" />
- </div>
- <div>
- <label>Search Area:</label>
- <select name="radius" id="radius">
- <option value="5">5 mi.</option>
- <option value="10">10 mi.</option>
- <option value="15">15 mi.</option>
- <option value="20">20 mi.</option>
- </select>
- </div>
- <div>
- <input type="hidden" name="submitted" value="submitted" />
- <input type="submit" value="Submit" />
- </div>
- </form>
- <?php
- if (isset ($shops)) {
- if (!empty ($shops)) {
- echo '<p><strong>' . count ($shops) . ' results were found.</strong></p>';
- foreach ($shops as $value) {
- echo '<p><strong>' . $value['name'] . '</strong><br />';
- echo $value['address'] . '<br />';
- echo $value['city'] . ', ' . $value['state'] . ' ' . $value['zip'];
- echo ' <a target="_blank" href="http://maps. google. com/maps?q=',
- $value['address'], ' ',
- $value['city'], ', ',
- $value['state'], ' ',
- $value['zip'],
- '">Map this location</a><br />';
- echo 'Phone: ' . $value['phone'];
- echo '</p>';
- }
- } else {
- echo '<p><strong>No results found</strong></p>';
- echo '<p><strong>' . $z . '</strong><br />';
- }
- }
- ?>
- </body>
- </html>
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
July 9th, 2012, 3:25 pm
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 2 posts
- Users browsing this forum: No registered users and 172 guests
- You cannot post new topics in this forum
- You cannot reply to topics in this forum
- You cannot edit your posts in this forum
- You cannot delete your posts in this forum
- You cannot post attachments in this forum
