Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 95 lines (72 sloc) 3.293 kb
85ae7d3 @pezholio Updating code to reflect live site state
authored
1 <?php
2 require_once("xmlparse.php");
3 require_once("phpcoord-2.3.php");
4 require_once("electoraldistrict.php");
5
6 $db_name = 'pstcdrz_postcode';
7 $db_username = 'pstcdrz_postcod';
8 $db_password = 'UnleashedX';
9
10 mysql_connect('localhost', $db_username, $db_password);
11 mysql_select_db($db_name) or die(mysql_error());
12
13 if ($_GET['postcode']) {
14
15 $postcode = mysql_real_escape_string($_GET['postcode']);
16
17 if ($postcode != strtoupper($postcode)) {
18 header ('HTTP/1.1 301 Moved Permanently');
19
20 if (strlen($_GET['format']) > 0) {
f368ce5 @ivebeenlinuxed Make code portable
ivebeenlinuxed authored
21 header("Location: /postcode/".strtoupper($postcode).".".$_GET['format']);
85ae7d3 @pezholio Updating code to reflect live site state
authored
22 } else {
f368ce5 @ivebeenlinuxed Make code portable
ivebeenlinuxed authored
23 header("Location: /postcode/".strtoupper($postcode));
85ae7d3 @pezholio Updating code to reflect live site state
authored
24 }
25 }
26
27 $result = mysql_query("SELECT * FROM postcodes WHERE REPLACE(postcode,' ','') = '$postcode'");
28
29 } else {
30 $lat = mysql_real_escape_string($_GET['lat']);
31 $lng = mysql_real_escape_string($_GET['lng']);
32 $result = mysql_query("SELECT *, ( 3959 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) AS distance FROM postcodes ORDER BY distance LIMIT 0,1");
33 $_GET['format'] = str_replace(".", "", $_GET['format']);
34 }
35
36 $single = TRUE;
37
38 $num_rows = mysql_num_rows($result);
39
40 if ($num_rows == 0) {
41 header("HTTP/1.0 404 Not Found");
42 include("404.php");
43 } else {
44
45 $row = mysql_fetch_array($result);
46
47 $lat = $row['lat'];
48 $lng = $row['lng'];
49
50 $ll2w = new LatLng($lat, $lng);
51 $ll2w->WGS84ToOSGB36();
52 $os2w = $ll2w->toOSRef();
53 $eastingnorthing = $os2w->toSplitString();
54
55 $easting = $eastingnorthing['easting'];
56 $northing = $eastingnorthing['northing'];
57
58 $geohash = file_get_contents("http://geohash.org?q=".$lat.",".$lng."&format=url");
59
60 if ($row['county'] != "00") {
61 $county = get_xml("http://statistics.data.gov.uk/doc/local-authority/". $row['county'] .".rdf");
62 $countytitle = $county['rdf:RDF']['rdf:Description'][0]['skos:prefLabel']['value'];
63 $countycode = $county['rdf:RDF']['rdf:Description'][0]['skos:notation']['value'];
64
65 $district = electoralDistrict($easting, $northing);
66 }
67
68 print_r($district);
69
70 $district = get_xml("http://statistics.data.gov.uk/doc/local-authority/". $row['county'] . $row['district'] .".rdf");
71 $districttitle = $district['rdf:RDF']['rdf:Description'][0]['skos:prefLabel']['value'];
72 $districtcode = $district['rdf:RDF']['rdf:Description'][0]['skos:notation']['value'];
73
74 $ward = get_xml("http://statistics.data.gov.uk/doc/electoral-ward/". $row['county'] . $row['district'] . $row['ward'] .".rdf");
75 $wardtitle = $ward['rdf:RDF']['rdf:Description'][0]['rdfs:label'][1]['value'];
76
77 if ($_GET['format'] == "xml" || $_SERVER['HTTP_ACCEPT'] == "application/xml") {
78 header ("content-type: application/xml");
79 include("xml.php");
80 } elseif ($_GET['format'] == "json" || $_SERVER['HTTP_ACCEPT'] == "application/json") {
81 header('Content-type: application/json');
82 include("json.php");
83 } elseif ($_GET['format'] == "rdf" || $_SERVER['HTTP_ACCEPT'] == "application/rdf+xml") {
84 header ("Content-type: application/rdf+xml");
85 include("rdf.php");
86 } elseif ($_GET['format'] == "csv" || $_SERVER['HTTP_ACCEPT'] == "text/csv") {
87 header("Content-type: application/octet-stream");
88 include("csv.php");
89 } elseif (strlen($_GET['format']) == 0) {
90 header ("content-type: text/html");
91 include("result.php");
92 }
93
94 }
95 ?>
Something went wrong with that request. Please try again.