Skip to content
Browse files

Merge pull request #1 from dracos/master

Improve postcode.php
  • Loading branch information...
2 parents 4839284 + 97cab37 commit 9f064a3e01fc25cee768edde054760701b03e6b5 @pezholio committed Jun 22, 2012
Showing with 76 additions and 72 deletions.
  1. +76 −72 postcode.php
View
148 postcode.php
@@ -12,31 +12,33 @@
$postcode = mysql_real_escape_string($_GET['postcode']);
-if ($postcode != strtoupper($postcode)) {
- header ('HTTP/1.1 301 Moved Permanently');
+ if ($postcode != strtoupper($postcode)) {
+ header ('HTTP/1.1 301 Moved Permanently');
- if (strlen($_GET['format']) > 0) {
- header("Location: http://www.uk-postcodes.com/postcode/".strtoupper($postcode).".".$_GET['format']);
- } else {
- header("Location: http://www.uk-postcodes.com/postcode/".strtoupper($postcode));
+ if (strlen($_GET['format']) > 0) {
+ header("Location: http://www.uk-postcodes.com/postcode/".strtoupper($postcode).".".$_GET['format']);
+ } else {
+ header("Location: http://www.uk-postcodes.com/postcode/".strtoupper($postcode));
+ }
+ exit;
}
-}
-if (strstr($postcode, " ")) {
- header ('HTTP/1.1 301 Moved Permanently');
+ if (strstr($postcode, " ")) {
+ header ('HTTP/1.1 301 Moved Permanently');
- if (strlen($_GET['format']) > 0) {
- header("Location: http://www.uk-postcodes.com/postcode/".str_replace(" ", "", $postcode).".".$_GET['format']);
- } else {
- header("Location: http://www.uk-postcodes.com/postcode/".str_replace(" ", "", $postcode));
+ if (strlen($_GET['format']) > 0) {
+ header("Location: http://www.uk-postcodes.com/postcode/".str_replace(" ", "", $postcode).".".$_GET['format']);
+ } else {
+ header("Location: http://www.uk-postcodes.com/postcode/".str_replace(" ", "", $postcode));
+ }
+ exit;
}
-}
-if (strlen($postcode) == 6) {
- $postcode = substr($postcode, 0, 3) ." ". substr($postcode, 3, 3);
-} elseif (strlen($postcode) == 5) {
- $postcode = substr($postcode, 0, 2) ." ". substr($postcode, 2, 3);
-}
+ if (strlen($postcode) == 6) {
+ $postcode = substr($postcode, 0, 3) ." ". substr($postcode, 3, 3);
+ } elseif (strlen($postcode) == 5) {
+ $postcode = substr($postcode, 0, 2) ." ". substr($postcode, 2, 3);
+ }
$result = mysql_query("SELECT postcode, lat, lng, easting, northing, postcodes.county AS countygss, counties.county AS countyname, counties.snacid AS countysnac, postcodes.council AS councilgss, councils.snacid AS councilsnac, councils.council AS councilname, postcodes.ward AS wardgss, wards.ward as wardname, wards.snacid as wardsnac, postcodes.constituency AS constituencygss, constituencies.constituency AS constituencyname, constituencies.snacid AS constituencycode, postcodes.parish AS parishgss, parishes.snacid AS parishsnac, parishes.parish AS parishname FROM postcodes LEFT JOIN councils ON postcodes.council = councils.code LEFT JOIN wards ON postcodes.ward = wards.code LEFT JOIN counties ON postcodes.county = counties.code LEFT JOIN constituencies ON postcodes.constituency = constituencies.code LEFT JOIN parishes ON postcodes.parish = parishes.code WHERE postcodes.postcode = '$postcode' LIMIT 0,1");
@@ -58,42 +60,44 @@
if ($num_rows == 0) {
header("HTTP/1.0 404 Not Found");
include("404.php");
-} else {
+ exit;
+}
- $row = mysql_fetch_array($result);
-
- $updatepostcode = $row['postcode'];
-
- if (!strstr(" ", $row['postcode'])) {
+$row = mysql_fetch_array($result);
+
+$updatepostcode = $row['postcode'];
+
+if (!strstr(" ", $row['postcode'])) {
$row['postcode'] = substr($row['postcode'], 0, 4) ." ". substr($row['postcode'], -3);
- }
-
- $lat = $row['lat'];
- $lng = $row['lng'];
-
- $easting = $row['easting'];
- $northing = $row['northing'];
-
- $geohash = file_get_contents("http://geohash.org?q=".$lat.",".$lng."&format=url");
+}
+
+$lat = $row['lat'];
+$lng = $row['lng'];
+$easting = $row['easting'];
+$northing = $row['northing'];
+
+$geohash = file_get_contents("http://geohash.org?q=".$lat.",".$lng."&format=url");
+
+$mapit = null;
if (!strstr($row['countygss'], "99999999")) {
-$countytitle = $row['countyname'];
-$countycode = $row['countysnac'];
+ $countytitle = $row['countyname'];
+ $countycode = $row['countysnac'];
if (strlen($row['electoraldistrict']) == 0) {
-
- $mapit = json_decode(file_get_contents("http://mapit.mysociety.org/postcode/". urlencode($postcode) .".json"));
-
+
+ $mapit = json_decode(file_get_contents("http://mapit.mysociety.org/postcode/". urlencode($updatepostcode) .".json"));
+
foreach ($mapit->areas as $area) {
if ($area->type_name == "County council ward") {
$edistrict['uri'] = "http://data.ordnancesurvey.co.uk/doc/7". str_pad($area->codes->unit_id, 15, "0", STR_PAD_LEFT);
$edistrict['code'] = $area->codes->unit_id;
$edistrict['name'] = $area->name;
}
}
-
- mysql_query("UPDATE postcodes SET electoraldistrict = '$edistrict[code]' WHERE postcode = '$updatepostcode'");
+
+ //mysql_query("UPDATE postcodes SET electoraldistrict = '$edistrict[code]' WHERE postcode = '$updatepostcode'");
}
}
@@ -105,53 +109,55 @@
// Fix for NI wards, who have the "GSS" code, not the Snac in URIs
if (strlen($row['wardgss']) == "6") {
-$wardcode = $row['wardgss'];
-$row['wardsnac'] = str_replace(" ", "_", $row['wardgss']);
+ $wardcode = $row['wardgss'];
+ $row['wardsnac'] = str_replace(" ", "_", $row['wardgss']);
} else {
-$wardcode = $row['wardsnac'];
+ $wardcode = $row['wardsnac'];
}
$constituencycode = $row['constituencycode'];
$constituencytitle = $row['constituencyname'];
if (strlen($constituencycode) == 3) {
-$ngnote = "<em>(Irish National Grid)</em>";
-$constituencyuri = "http://statistics.data.gov.uk/doc/parliamentary-constituency/". $constituencycode ;
+ $ngnote = "<em>(Irish National Grid)</em>";
+ $constituencyuri = "http://statistics.data.gov.uk/doc/parliamentary-constituency/". $constituencycode ;
} else {
-$constituencyuri = "http://data.ordnancesurvey.co.uk/doc/7". str_pad($constituencycode, 15, "0", STR_PAD_LEFT);
+ $constituencyuri = "http://data.ordnancesurvey.co.uk/doc/7". str_pad($constituencycode, 15, "0", STR_PAD_LEFT);
}
-if (strlen($row['parish'] == 0)) {
+if (strlen($row['parishgss'] == 0)) {
-$mapit = json_decode(file_get_contents("http://mapit.mysociety.org/postcode/". $postcode .".json"));
+ if (!$mapit) {
+ $mapit = json_decode(file_get_contents("http://mapit.mysociety.org/postcode/". urlencode($updatepostcode) .".json"));
+ }
-foreach ($mapit->areas as $area) {
- if ($area->type_name == "Civil Parish") {
- $parishcode = $area->codes->ons;
- $parishuri = "http://data.ordnancesurvey.co.uk/doc/7". str_pad($area->codes->unit_id, 15, "0", STR_PAD_LEFT);
- $parishname = $area->name;
+ foreach ($mapit->areas as $area) {
+ if ($area->type_name == "Civil Parish") {
+ $parishcode = $area->codes->ons;
+ $parishuri = "http://data.ordnancesurvey.co.uk/doc/7". str_pad($area->codes->unit_id, 15, "0", STR_PAD_LEFT);
+ $parishname = $area->name;
+ }
}
-}
-mysql_query("UPDATE postcodes SET parish = '$parishcode' WHERE postcode = '$updatepostcode'");
+ mysql_query("UPDATE postcodes SET parish = '$parishcode' WHERE postcode = '$updatepostcode'");
} else {
-$parishname = $row['parishname'];
-$parishcode = $row['parishsnac'];
+ $parishname = $row['parishname'];
+ $parishcode = $row['parishsnac'];
}
- // Santa Easter Egg
-
- if ($row['postcode'] == "SAN TA1") {
- $row['postcode'] = "SAN TA1";
- $constituencytitle = "North Pole";
- $constituencyuri = "http://www.northpole.com/";
- $districttitle = "Reindeerland";
- $row['countygss'] = "99999999";
- $wardtitle = "Santa’s Grotto";
- $easting = "N/A";
- $northing = "N/A";
- }
+// Santa Easter Egg
+
+if ($row['postcode'] == "SAN TA1") {
+ $row['postcode'] = "SAN TA1";
+ $constituencytitle = "North Pole";
+ $constituencyuri = "http://www.northpole.com/";
+ $districttitle = "Reindeerland";
+ $row['countygss'] = "99999999";
+ $wardtitle = "Santa’s Grotto";
+ $easting = "N/A";
+ $northing = "N/A";
+}
if ($_GET['format'] == "xml" || $_SERVER['HTTP_ACCEPT'] == "application/xml") {
header ("content-type: application/xml");
@@ -171,5 +177,3 @@
include("result.php");
}
-}
-?>

0 comments on commit 9f064a3

Please sign in to comment.
Something went wrong with that request. Please try again.