CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2012
    Posts
    8

    PHP/mySQL Zip Code Distance Locator

    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://htmlcampus.com/build-a-zip-co...cation-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).

    Below is my code for the page (password left out). I hope someone could spot what might be wrong, something I missed.
    Code:
    <?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 ('atrs') 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>
    Last edited by amiecutie; June 26th, 2014 at 11:02 AM.

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: PHP/mySQL Zip Code Distance Locator

    Does it work if you change it back to "zip_codes" and the code that you changed along with that? If so...then you're simply missing a reference to "zip_codes".
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured