Jump to content

Rudiger

New Members
  • Posts

    1
  • Joined

  • Last visited

    Never

Everything posted by Rudiger

  1. I have a script to find a User’s nearest store depending on their postcode. The postcodes being used here are American zip codes. Would it be possible to alter this script so that it returns the Users’ nearest store depending on the UK postcode they have entered? I have tried to do this by changing a few things such as changing 'int' to 'string' and 'is_numeric' to 'is_string' but cant seem to crack it. Here is the working script for finding a Users' nearest store - store_locator.php. <?php // Create page variables $r = NULL; $z = NULL; $stores = NULL; $Errors = NULL; $conn=mysqli_connect('localhost', 'username', 'password') or die ("No connection"); mysqli_select_db($conn, 'username') or die ("test will not open"); // Declare page functions function Dist ($lat1, $lon1, $lat2, $lon2) { $distance = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($lon1 - $lon2)); $distance = (rad2deg(acos($distance))) * 69.09; return $distance; } ### Handle form if submitted if (isset ($_POST['submitted'])) { // Validate Postcode code field if (!empty ($_POST['zip']) && is_numeric ($_POST['zip'])) { $z = (int)$_POST['zip']; // Verify Postcode code exists $query = "SELECT lat, lon FROM zip_codes WHERE zip = '$z'"; //$result = mysql_query ($query); $result=mysqli_query($conn, $query) or die ('Invalid query.'); if (mysqli_num_rows ($result) == 1) { $zip = mysqli_fetch_assoc ($result); } else { $Errors = '<p>The postcode code you entered was not found!</p>'; $z=NULL; } } // 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 locations $stores = array(); $query = "SELECT name, address, town, state, postal, phone, hours, lat, lon FROM stores INNER JOIN zip_codes ON stores.postal = zip_codes.zip"; $result = mysqli_query ($conn, $query); // Go through and check all locations while ($row = mysqli_fetch_assoc ($result)) { // Separate closest locations $distance = Dist ($row['lat'], $row['lon'], $zip['lat'], $zip['lon']); // Check if store is in radius if ($distance <= $r) { $stores[] = array ( 'name' => $row['name'], 'address' => $row['address'], 'state' => $row['state'], 'town' => $row['town'], 'postal' => $row['postal'], 'phone' => $row['phone'], 'hours' => $row['hours'] ); } } } else { $Errors = ($Errors) ? $Errors : '<p>Errors were found please try again!</p>'; } } ?><html> <head> <title>Store Locator</title> </head> <body> <form action="" method="post"> <p>Enter your zip code below to find locations near you.</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 ($stores)) { if (!empty ($stores)) { echo '<p><strong>' . count ($stores) . ' results were found.</strong></p>'; foreach ($stores as $value) { echo '<p><strong>' . $value['name'] . '</strong><br />'; echo $value['address'] . '<br />'; echo $value['town'] . ', ' . $value['state'] . ' ' . $value['postal']; echo ' <a target="_blank" href="http://maps.google.com/maps?q=', $value['address'], ' ', $value['town'], ', ', $value['state'], ' ', $value['postal'], '">Map this location</a><br />'; echo 'Phone: ' . $value['phone'] . '<br />'; echo 'Hours: ' . $value['hours']; echo '</p>'; } } else { echo '<p><strong>No results found</strong></p>'; } } ?> </body> </html> The SQL used to create the tables is as follows: CREATE TABLE `stores` ( `store_id` INT(11) unsigned NOT NULL AUTO_INCREMENT, `name` VARCHAR(50) NOT NULL, `address` VARCHAR(50) NOT NULL, `town` VARCHAR(25) NOT NULL, `state` VARCHAR(50) NOT NULL, `postal` VARCHAR(5) NOT NULL, `phone` VARCHAR(20) DEFAULT NULL, `hours` VARCHAR(100) DEFAULT NULL, PRIMARY KEY (`store_id`), KEY `postal` (`postal`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; /*Data for the table `stores` */ INSERT INTO `stores`(`store_id`,`name`,`address`,`town`,`state`,`postal`,`phone`,`hours`) VALUES (1,'Main Shop','123 Church Street.','New York','New York','10007','(212) 992-3399','Mon-Fri: 9-5, Sat: 8-6, Sun: Closed'), (2,'Park Avenue Store','410 Park Avenue','New York','New York','10006','(212) 480-4400','Mon-Fri: 9-5, Sat: 8-6, Sun: Closed'), (3,'Central Park Store','50 Central Park West','New York','New York','10023','(212) 537-3294','Mon-Fri: 9-5, Sat: 8-6, Sun: Closed'), (4,'SOHO Store','311 East Houston Street','New York','New York','10005','(212) 545-5540','Mon-Fri: 9-5, Sat: 8-6, Sun: Closed'), (5,'Wall Street Store','110 Wall Street.','New York','New York','10014','(212) 210-0000','Mon-Fri: 9-5, Sat: 8-6, Sun: Closed'), (6,'Times Square Store','53 West 42nd Street','New York','New York','10036','(212) 646-1000','Mon-Fri: 9-5, Sat: 8-6, Sun: Closed'); CREATE TABLE IF NOT EXISTS `zip_codes` ( `zip_id` int(11) NOT NULL AUTO_INCREMENT, `zip` varchar(5) NOT NULL DEFAULT '', `abbr_state` char(2) NOT NULL DEFAULT '', `lat` varchar(10) NOT NULL DEFAULT '', `lon` varchar(10) NOT NULL DEFAULT '', `city` varchar(50) DEFAULT NULL, `full_state` varchar(50) DEFAULT NULL, PRIMARY KEY (`zip_id`), UNIQUE KEY `zip` (`zip`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ; -- -- Dumping data for table `zip_codes` -- INSERT INTO `zip_codes` (`zip_id`, `zip`, `abbr_state`, `lat`, `lon`, `city`, `full_state`) VALUES (1, '10007', 'NY', '40.714754', '-74.00721', 'New York', 'New York'), (2, '10006', 'NY', '40.707904', '-74.01342', 'New York', 'New York'), (3, '10023', 'NY', '40.776099', '-73.98285', 'New York', 'New York'), (4, '10005', 'NY', '40.706019', '-74.00858', 'New York', 'New York'), (5, '10014', 'NY', '40.734718', '-74.00532', 'New York', 'New York'), (6, '10036', 'NY', '40.759511', '-73.99019', 'New York', 'New York'); Any help is much appreciated!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.