/ / Merci de faire avancer ce sur le forum de codage unflux.
Ok, donc après quelques téléchargement, de déconner dans Excel, et codant pour un certain temps lil, I got a solution va...
Vous pouvez voir le script et exécute ici .
Il ya une fonction dans le script qui n'est pas à moi (et je l'ai étiquetées comme telles), et comme il a travaillé, je n'ai pas pris la peine de le retravailler recode ou du tout).
Ce n'est tout simplement comme une démonstration de la façon dont il peut être fait. Il n'ya aucune vérification d'erreur, donc il tomber si vous entrez autre chose que d'une paire de codes valides zip

Heres le code actuel pour le faire...(BWM,
s'il vous plaît install [php], hein)
enregistrer en tant que distance.php (ou modifier la forme pour tenir compte de nouveau nom de fichier)
<?php
$dbhost = 'localhost';
$dbname = 'database_name';
$dbuname = 'account_name';
$dbupass = 'account_password';
$connection = mysql_connect("$dbhost","$dbuname","$dbupass")
or die ("Couldn't connect to server.");
$db = mysql_select_db("$dbname", $connection)
or die("Couldn't select database.");
// I did not write this function - I found it on the web somewhere.
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtolower($unit);
if ($unit == "k") {
return ($miles * 1.609344);
} else if ($unit == "n") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
function page_header() {
return '<html>
<head>
<title>Distance Calculator</title>
<style>
<!--
input { font-family: Verdana; font-size: 8pt; font-weight: bold }
td { font-family: Verdana; font-size: 8pt; font-weight: bold }
-->
</style>
</head>
<body>
<center><table border="0" width="350" cellspacing="0" cellpadding="0">
<tr><td align="center"><u>Axe\'s Distance Calculator</u><br><br></td></tr>
<tr><td>';
}
function page_footer() {
return '</td></tr></table></center></body></html>';
}
function diststuff($zipo, $zipd) {
$origsql = 'select * from zipcodes where zip = "'.$zipo.'"';
$origresult = mysql_query($origsql);
$origdata = mysql_fetch_assoc($origresult);
$destsql = 'select * from zipcodes where zip = "'.$zipd.'"';
$destresult = mysql_query($destsql);
$destdata = mysql_fetch_assoc($destresult);
return '<table border="0" width="350" cellspacing="0" cellpadding="0">
<tr>
<td nowrap>Originating zip code :</td>
<td align="right" nowrap>'.$origdata['zip'].', '.$origdata['state'].'</td>
</tr>
<tr>
<td nowrap>Destination zip code :</td>
<td align="right" nowrap>'.$destdata['zip'].', '.$destdata['state'].'</td>
</tr>
<tr>
<td nowrap>Distance :</td>
<td align="right" nowrap>'.number_format(distance($origdata['lat'], $origdata['long'], $destdata['lat'], $destdata['long'], "m"), 2).' Miles</td>
</tr>
<tr><td colspan="2"><hr></td></tr>
</table>';
}
function zipform() {
return '<table border="0" width="350" cellspacing="0" cellpadding="0">
<form method="POST" action="distance.php">
<tr>
<td nowrap>Enter originating zip code :</td>
<td align="right" nowrap><input type="text" name="origzip" size="20" value="'.$_POST['origzip'].'"></td>
</tr>
<tr>
<td nowrap>Enter destination zip code :</td>
<td align="right" nowrap><input type="text" name="destzip" size="20" value="'.$_POST['destzip'].'"></td>
</tr>
<tr>
<td colspan="2" align="right" nowrap><input type="submit" value="Submit" name="B1">
<input type="reset" value="Reset" name="B2"></td>
</tr>
</form>
</table>';
}
echo page_header();
if ((!$origzip) || (!$destzip)) {
echo zipform();
} else if (($origzip) && ($destzip)) {
echo diststuff($_POST['origzip'], $_POST['destzip']);
echo zipform();
}
echo page_footer();
?>
- <?php
- $dbhost = 'localhost';
- $dbname = 'database_name';
- $dbuname = 'account_name';
- $dbupass = 'account_password';
- $connection = mysql_connect("$dbhost","$dbuname","$dbupass")
- or die ("Couldn't connect to server.");
-
- $db = mysql_select_db("$dbname", $connection)
- or die("Couldn't select database.");
- // I did not write this function - I found it on the web somewhere.
- function distance($lat1, $lon1, $lat2, $lon2, $unit) {
- $theta = $lon1 - $lon2;
- $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
- $dist = acos($dist);
- $dist = rad2deg($dist);
- $miles = $dist * 60 * 1.1515;
- $unit = strtolower($unit);
- if ($unit == "k") {
- return ($miles * 1.609344);
- } else if ($unit == "n") {
- return ($miles * 0.8684);
- } else {
- return $miles;
- }
- }
- function page_header() {
- return '<html>
- <head>
- <title>Distance Calculator</title>
- <style>
- <!--
- input { font-family: Verdana; font-size: 8pt; font-weight: bold }
- td { font-family: Verdana; font-size: 8pt; font-weight: bold }
- -->
- </style>
- </head>
- <body>
- <center><table border="0" width="350" cellspacing="0" cellpadding="0">
- <tr><td align="center"><u>Axe\'s Distance Calculator</u><br><br></td></tr>
- <tr><td>';
- }
- function page_footer() {
- return '</td></tr></table></center></body></html>';
- }
- function diststuff($zipo, $zipd) {
- $origsql = 'select * from zipcodes where zip = "'.$zipo.'"';
- $origresult = mysql_query($origsql);
- $origdata = mysql_fetch_assoc($origresult);
- $destsql = 'select * from zipcodes where zip = "'.$zipd.'"';
- $destresult = mysql_query($destsql);
- $destdata = mysql_fetch_assoc($destresult);
- return '<table border="0" width="350" cellspacing="0" cellpadding="0">
- <tr>
- <td nowrap>Originating zip code :</td>
- <td align="right" nowrap>'.$origdata['zip'].', '.$origdata['state'].'</td>
- </tr>
- <tr>
- <td nowrap>Destination zip code :</td>
- <td align="right" nowrap>'.$destdata['zip'].', '.$destdata['state'].'</td>
- </tr>
- <tr>
- <td nowrap>Distance :</td>
- <td align="right" nowrap>'.number_format(distance($origdata['lat'], $origdata['long'], $destdata['lat'], $destdata['long'], "m"), 2).' Miles</td>
- </tr>
- <tr><td colspan="2"><hr></td></tr>
- </table>';
- }
- function zipform() {
- return '<table border="0" width="350" cellspacing="0" cellpadding="0">
- <form method="POST" action="distance.php">
- <tr>
- <td nowrap>Enter originating zip code :</td>
- <td align="right" nowrap><input type="text" name="origzip" size="20" value="'.$_POST['origzip'].'"></td>
- </tr>
- <tr>
- <td nowrap>Enter destination zip code :</td>
- <td align="right" nowrap><input type="text" name="destzip" size="20" value="'.$_POST['destzip'].'"></td>
- </tr>
- <tr>
- <td colspan="2" align="right" nowrap><input type="submit" value="Submit" name="B1">
- <input type="reset" value="Reset" name="B2"></td>
- </tr>
- </form>
- </table>';
- }
- echo page_header();
- if ((!$origzip) || (!$destzip)) {
- echo zipform();
- } else if (($origzip) && ($destzip)) {
- echo diststuff($_POST['origzip'], $_POST['destzip']);
- echo zipform();
- }
- echo page_footer();
- ?>
Ainsi, theres un bouquet de prettying inutile-up, heh. Mais je voulais que pour produire des sorties assez n neat tidy gif "alt =":)" title =" Smile "/>
Heres le code SQL pour créer la table de base de données (Yeah, généré par phpMyAdmin, hein)...
CREATE TABLE `zipcodes` (
`state` char(2) NOT NULL default '',
`zip` varchar(5) NOT NULL default '',
`lat` varchar(15) NOT NULL default '',
`long` varchar(15) NOT NULL default '',
KEY `zip` (`zip`)
) TYPE=MyISAM;
- CREATE TABLE `zipcodes` (
- `state` char(2) NOT NULL default '',
- `zip` varchar(5) NOT NULL default '',
- `lat` varchar(15) NOT NULL default '',
- `long` varchar(15) NOT NULL default '',
- KEY `zip` (`zip`)
- ) TYPE=MyISAM;
Et
Voici toutes les données du code postal dans un fichier CSV que vous pouvez importer directement dans la base de données via phpMyAdmin, ou la ligne cmmand si vous savez comment .
Le CSV a été créé à partir des données
ce fichier texte . Ce fichier contient également tout un tas de bits autres proprette de info (population, les maisons, les terres et les zones d'eau en mètres carrés & sq miles (au cas où vous souhaitez créer votre propre fichier CSV et tables et ajouter les morceaux de ce script).
Ce fichier, avec un tas d'autres personnes est
disponible ici ..
Thats essentiellement, qu'à configurer la base de données, de modifier les variables de base de données nom d'hôte, nom de base de données, base de données nom d'utilisateur et mot de passe, télécharger le script, et c'est parti. gif "alt =":)" title =" Smile "/>