PHP/MySQL Zip Shop Finder

  • AmieCutie
  • Newbie
  • Newbie
  • User avatar
  • Posts: 12

Post 3+ Months Ago

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.
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 '&nbsp;<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>
 
  1. <?php
  2.  
  3. // Create page variables
  4. $r = NULL;
  5. $z = NULL;
  6. $shops = NULL;
  7. $Errors = NULL;
  8.  
  9. // Establish DB connection
  10. mysql_connect ('localhost', 'acq-admin', 'password') or die(mysql_error());
  11. mysql_select_db ('autocareq') or die(mysql_error());
  12.  
  13. // Declare page functions
  14. function Dist ($lat_A, $long_A, $lat_B, $long_B) {
  15.  
  16.   $distance = sin(deg2rad($lat_A))
  17.       * sin(deg2rad($lat_B))
  18.       + cos(deg2rad($lat_A))
  19.       * cos(deg2rad($lat_B))
  20.       * cos(deg2rad($long_A - $long_B));
  21.  
  22.   $distance = (rad2deg(acos($distance))) * 69.09;
  23.   return $distance;
  24.  
  25. }
  26.  
  27. ### Handle form if submitted
  28. if (isset ($_POST['submitted'])) {
  29.  
  30.   // Validate Zip code field
  31.   if (!empty ($_POST['zip']) && is_numeric ($_POST['zip'])) {
  32.  
  33.     $z = (int)$_POST['zip'];
  34.  
  35.     // Verify zip code exists
  36.     $query = "SELECT lat, lon FROM locations WHERE zip = '$z'";
  37.     $result = mysql_query ($query);
  38.  
  39.     if (mysql_num_rows ($result) == 1) {
  40.       $zip = mysql_fetch_assoc ($result);
  41.     } else {
  42.       $Errors = '<p>The zip code you entered was not found!</p>';
  43.     }
  44.  
  45.   }
  46.  
  47.   // Validate radius field
  48.   if (isset ($_POST['radius']) && is_numeric ($_POST['radius'])) {
  49.     $r = (int)$_POST['radius'];
  50.   }
  51.  
  52.   // Proceed if no errors were found
  53.   if ($r && $z) {
  54.  
  55.     // Retrieve coordinates of the shops
  56.     $shops = array();
  57.     $query = "SELECT name, address, city, state, zip, phone, lat, lon
  58.    FROM dealerships
  59.    INNER JOIN locations
  60.    ON dealerships.zip = locations.zip";
  61.     $result = mysql_query ($query);
  62.  
  63.     // Go through and check all shops
  64.     while ($row = mysql_fetch_assoc ($result)) {
  65.  
  66.       // Separate closest shops
  67.       $distance = Dist ($row['lat'], $row['lon'], $zip['lat'], $zip['lon']);
  68.  
  69.       // Check if shop is in radius
  70.       if ($distance <= $r) {
  71.  
  72.         $shops[] = array (
  73.           'name'      => $row['name'],
  74.           'address'   => $row['address'],
  75.           'state'     => $row['state'],
  76.           'city'      => $row['city'],
  77.           'zip'       => $row['zip'],
  78.           'phone'     => $row['phone']
  79.         );
  80.  
  81.       }
  82.  
  83.     }
  84.  
  85.   } else {
  86.     $Errors = ($Errors) ? $Errors : '<p>Errors were found please try again!</p>';
  87.   }
  88.  
  89. }
  90.  
  91. ?><html>
  92. <head>
  93. <title>Pick Your Shop</title>
  94. </head>
  95. <body>
  96. <form action="" method="post">
  97.   <p>Enter your zip code below to find the nearest shop</p>
  98.   <?php echo ($Errors) ? $Errors : ''; ?>
  99.   <div>
  100.     <label>Zip:</label>
  101.     <input name="zip" type="text" size="10" maxlength="5" />
  102.   </div>
  103.   <div>
  104.     <label>Search Area:</label>
  105.     <select name="radius" id="radius">
  106.       <option value="5">5 mi.</option>
  107.       <option value="10">10 mi.</option>
  108.       <option value="15">15 mi.</option>
  109.       <option value="20">20 mi.</option>
  110.     </select>
  111.   </div>
  112.   <div>
  113.     <input type="hidden" name="submitted" value="submitted" />
  114.     <input type="submit" value="Submit" />
  115.   </div>
  116. </form>
  117.  
  118. <?php
  119.  
  120. if (isset ($shops)) {
  121.  
  122.   if (!empty ($shops)) {
  123.  
  124.     echo '<p><strong>' . count ($shops) . ' results were found.</strong></p>';
  125.     foreach ($shops as $value) {
  126.  
  127.       echo '<p><strong>' . $value['name'] . '</strong><br />';
  128.       echo $value['address'] . '<br />';
  129.       echo $value['city'] . ', ' . $value['state'] . ' ' . $value['zip'];
  130.       echo '&nbsp;<a target="_blank" href="http://maps. google. com/maps?q=',
  131.       $value['address'], ' ',
  132.       $value['city'], ', ',
  133.       $value['state'], ' ',
  134.       $value['zip'],
  135.       '">Map this location</a><br />';
  136.       echo 'Phone: ' . $value['phone'];
  137.       echo '</p>';
  138.  
  139.     }
  140.  
  141.   } else {
  142.     echo '<p><strong>No results found</strong></p>';
  143.     echo '<p><strong>' . $z . '</strong><br />';
  144.   }
  145.  
  146. }
  147.  
  148. ?>
  149. </body>
  150. </html>
  151.  
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • AmieCutie
  • Newbie
  • Newbie
  • User avatar
  • Posts: 12

Post 3+ Months Ago

Here are some captures from phpMyAdmin. There's just a few dummy shops for testing purposes.

http://imgurDOTcom/a/Nz7ii

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 75 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.