M
MW
Found this great store locator script, tweaked it - works great -
except:
Having trouble getting it to sort by distance ($dist). I'm sure I'm
missing something stupid and thought I could get a second pair of eyes
on it to tell me what I'm missing.
#!/usr/bin/perl -w
# ------------------------------------
# Store Locator
# ------------------------------------
print "Content-Type: text/html\n\n" ;
# Retrieve zipcode from form variable.
read(STDIN, $input, $ENV{'CONTENT_LENGTH'});
foreach (split(/&/, $input))
{
($NAME, $VALUE) = split(/=/, $_);
$NAME =~ s/\+/ /g;
$NAME =~ s/%([0-9|A-F]{2})/pack(C,hex($1))/eg;
$VALUE =~ s/\+/ /g;
$VALUE =~ s/%([0-9|A-F]{2})/pack(C,hex($1))/eg;
}
$zipcode = $VALUE;
# Open DBI connection with MySQL.
use DBI;
$dbh = DBI->connect("DBI:mysql:mydb;", "myid", "mypwd")
or die "Can't connect: $_";
# Set PI and radius in miles of results to show.
$PI = 3.1415926;
$radius = 50;
# Prepare SQL.
$sth = $dbh->prepare("select city, state, latitude, longitude " .
"from zipcodes where zipcode = '$zipcode'")
or die "Can't prepare zipcode: $dbh->errstr\n";
# Execute SQL.
$rv = $sth->execute
or die "Can't execute the query: $sth->errstr\n";
($city, $state, $latitude, $longitude) = $sth->fetchrow_array;
$sth = $dbh->prepare("select store_name, store_phone, store_address1,
store_city, store_state, store_zipcode," .
" latitude, longitude from stores, zipcodes ".
"where stores.store_zipcode = zipcodes.zipcode");
$rv = $sth->execute
or die "Can't execute the query: $sth->errstr\n";
undef %store_distances;
while (($name, $s_phone, $s_address, $s_city, $s_state, $s_zipcode,
$s_lat, $s_long) = $sth->fetchrow_array)
{
# Calculate distance.
$prodsin = sin($latitude) * sin($s_lat);
$prodcos = cos($latitude) * cos($s_lat);
$deltalong = cos(abs($s_long-$longitude));
$dist = &acos($prodsin + ($prodcos * $deltalong));
$dist = $dist * (180.0/$PI) * 69.0;
$store_distances{"$name|$s_address|$s_city|$s_state|$s_zipcode|$s_phone"}
= $dist;
}
# Sort the array.
# @ordered = sort {$store_distances{$a} > $store_distances{$b}}
keys(%store_distances);
@ordered = sort keys(%store_distances);
# Print page header.
print "<html>" .
"<head>" .
"<title>" ........
# Print each record.
foreach $item (@ordered)
{
if ($store_distances{$item} <= $radius)
{
($s_name, $s_address, $s_city, $s_state, $s_zipcode, $s_phone) =
split(/\|/, $item);
print "<tr>" .
"<td>".
$s_name .
"</td>" .
"<td>" .
$s_address . "<br />" .
"$s_city, $s_state $s_zipcode <br />" .
"<a href=\"http://maps.yahoo.com/py/maps.py?Pyt=Tmap&addr=" .
$s_address .
"&csz=" .
$s_zipcode .
"&Get%A0Map=Get+Map\" target=\"_blank\" title=\"Show me a map of
this address.\">Map</a>" .
"</td>" .
"<td nowrap>" .
$s_phone .
"</td>" .
"<td align=\"right\">" .
sprintf("%d", $store_distances{$item}) .
"</td>" .
"</tr>";
}
}
# Print page footer.
print "<tr><td colspan=\"4\" align=\"center\"> </td></tr>" .
"</body>" .
"</html>";
sub acos
{
($val) = @_;
return(atan2(sqrt(1-($val * $val)),$val));
}
sub asin
{
($val) = @_;
return(atan2($val, sqrt(1-($val * $val))));
}
except:
Having trouble getting it to sort by distance ($dist). I'm sure I'm
missing something stupid and thought I could get a second pair of eyes
on it to tell me what I'm missing.
#!/usr/bin/perl -w
# ------------------------------------
# Store Locator
# ------------------------------------
print "Content-Type: text/html\n\n" ;
# Retrieve zipcode from form variable.
read(STDIN, $input, $ENV{'CONTENT_LENGTH'});
foreach (split(/&/, $input))
{
($NAME, $VALUE) = split(/=/, $_);
$NAME =~ s/\+/ /g;
$NAME =~ s/%([0-9|A-F]{2})/pack(C,hex($1))/eg;
$VALUE =~ s/\+/ /g;
$VALUE =~ s/%([0-9|A-F]{2})/pack(C,hex($1))/eg;
}
$zipcode = $VALUE;
# Open DBI connection with MySQL.
use DBI;
$dbh = DBI->connect("DBI:mysql:mydb;", "myid", "mypwd")
or die "Can't connect: $_";
# Set PI and radius in miles of results to show.
$PI = 3.1415926;
$radius = 50;
# Prepare SQL.
$sth = $dbh->prepare("select city, state, latitude, longitude " .
"from zipcodes where zipcode = '$zipcode'")
or die "Can't prepare zipcode: $dbh->errstr\n";
# Execute SQL.
$rv = $sth->execute
or die "Can't execute the query: $sth->errstr\n";
($city, $state, $latitude, $longitude) = $sth->fetchrow_array;
$sth = $dbh->prepare("select store_name, store_phone, store_address1,
store_city, store_state, store_zipcode," .
" latitude, longitude from stores, zipcodes ".
"where stores.store_zipcode = zipcodes.zipcode");
$rv = $sth->execute
or die "Can't execute the query: $sth->errstr\n";
undef %store_distances;
while (($name, $s_phone, $s_address, $s_city, $s_state, $s_zipcode,
$s_lat, $s_long) = $sth->fetchrow_array)
{
# Calculate distance.
$prodsin = sin($latitude) * sin($s_lat);
$prodcos = cos($latitude) * cos($s_lat);
$deltalong = cos(abs($s_long-$longitude));
$dist = &acos($prodsin + ($prodcos * $deltalong));
$dist = $dist * (180.0/$PI) * 69.0;
$store_distances{"$name|$s_address|$s_city|$s_state|$s_zipcode|$s_phone"}
= $dist;
}
# Sort the array.
# @ordered = sort {$store_distances{$a} > $store_distances{$b}}
keys(%store_distances);
@ordered = sort keys(%store_distances);
# Print page header.
print "<html>" .
"<head>" .
"<title>" ........
# Print each record.
foreach $item (@ordered)
{
if ($store_distances{$item} <= $radius)
{
($s_name, $s_address, $s_city, $s_state, $s_zipcode, $s_phone) =
split(/\|/, $item);
print "<tr>" .
"<td>".
$s_name .
"</td>" .
"<td>" .
$s_address . "<br />" .
"$s_city, $s_state $s_zipcode <br />" .
"<a href=\"http://maps.yahoo.com/py/maps.py?Pyt=Tmap&addr=" .
$s_address .
"&csz=" .
$s_zipcode .
"&Get%A0Map=Get+Map\" target=\"_blank\" title=\"Show me a map of
this address.\">Map</a>" .
"</td>" .
"<td nowrap>" .
$s_phone .
"</td>" .
"<td align=\"right\">" .
sprintf("%d", $store_distances{$item}) .
"</td>" .
"</tr>";
}
}
# Print page footer.
print "<tr><td colspan=\"4\" align=\"center\"> </td></tr>" .
"</body>" .
"</html>";
sub acos
{
($val) = @_;
return(atan2(sqrt(1-($val * $val)),$val));
}
sub asin
{
($val) = @_;
return(atan2($val, sqrt(1-($val * $val))));
}