Skip to content

Instantly share code, notes, and snippets.

@pezholio
Created May 24, 2010 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pezholio/412040 to your computer and use it in GitHub Desktop.
Save pezholio/412040 to your computer and use it in GitHub Desktop.
An example project using Lichfield District Council open data to show a user's councillors and ward
<?php
// Two Custom functions to parse XML from external feeds. The first one is used to return an associative array from an XML document, and the second one gets the data via CURL and returns the array. The rest is up to you!!
function xml2array($contents, $get_attributes=1) {
if(!$contents) return array();
if(!function_exists('xml_parser_create')) {
//print "'xml_parser_create()' function not found!";
return array();
}
//Get the XML parser of PHP - PHP must have this module for the parser to work
$parser = xml_parser_create();
xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
xml_parser_set_option( $parser, XML_OPTION_SKIP_WHITE, 1 );
xml_parse_into_struct( $parser, $contents, $xml_values );
xml_parser_free( $parser );
if(!$xml_values) return;//Hmm...
//Initializations
$xml_array = array();
$parents = array();
$opened_tags = array();
$arr = array();
$current = &$xml_array;
//Go through the tags.
foreach($xml_values as $data) {
unset($attributes,$value);//Remove existing values, or there will be trouble
//This command will extract these variables into the foreach scope
// tag(string), type(string), level(int), attributes(array).
extract($data);//We could use the array by itself, but this cooler.
$result = '';
if($get_attributes) {//The second argument of the function decides this.
$result = array();
if(isset($value)) $result['value'] = $value;
//Set the attributes too.
if(isset($attributes)) {
foreach($attributes as $attr => $val) {
if($get_attributes == 1) $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
/** :TODO: should we change the key name to '_attr'? Someone may use the tagname 'attr'. Same goes for 'value' too */
}
}
} elseif(isset($value)) {
$result = $value;
}
//See tag status and do the needed.
if($type == "open") {//The starting of the tag '<tag>'
$parent[$level-1] = &$current;
if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
$current[$tag] = $result;
$current = &$current[$tag];
} else { //There was another element with the same tag name
if(isset($current[$tag][0])) {
array_push($current[$tag], $result);
} else {
$current[$tag] = array($current[$tag],$result);
}
$last = count($current[$tag]) - 1;
$current = &$current[$tag][$last];
}
} elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
//See if the key is already taken.
if(!isset($current[$tag])) { //New Key
$current[$tag] = $result;
} else { //If taken, put all things inside a list(array)
if((is_array($current[$tag]) and $get_attributes == 0)//If it is already an array...
or (isset($current[$tag][0]) and is_array($current[$tag][0]) and $get_attributes == 1)) {
array_push($current[$tag],$result); // ...push the new element into that array.
} else { //If it is not an array...
$current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
}
}
} elseif($type == 'close') { //End of tag '</tag>'
$current = &$parent[$level-1];
}
}
return($xml_array);
}
function get_xml($url) {
$ch = curl_init($url);
curl_setopt($ch, curlOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
$news = xml2array($data);
return $news;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Find my councillor</title>
<link type="text/css" rel="stylesheet" href="http://www.blueprintcss.org/blueprint/src/grid.css" />
<link type="text/css" rel="stylesheet" href="http://www.blueprintcss.org/blueprint/src/typography.css" />
<link type="text/css" rel="stylesheet" href="http://www.blueprintcss.org/blueprint/src/forms.css" />
<style type="text/css">
.btn {
display:inline-block;
background:none;
margin:0;
padding:3px 0;
border-width:0;
overflow:visible;
font:100%/1.2 Arial,Sans-serif;
text-decoration:none;
color:#333;
}
* html button.btn {
padding-bottom:1px;
}
/* Immediately below is a temporary hack to serve the
following margin values only to Gecko browsers
Gecko browsers add an extra 3px of left/right
padding to button elements which can't be overriden.
Thus, we use -3px of left/right margin to overcome this. */
html:not([lang*=""]) button.btn {
margin:0 -3px;
}
.btn span {
background:#ddd url(http://stopdesign.com/eg/buttons/3.0/img/bg-button.gif) repeat-x 0 0;
margin:0;
padding:3px 0;
border-left:1px solid #bbb;
border-right:1px solid #aaa;
}
* html .btn span {
padding-top:0;
}
.btn span span {
position:relative;
padding:3px .4em;
border-width:0;
border-top:1px solid #bbb;
border-bottom:1px solid #aaa;
}
ul {
margin: 0;
padding: 0;
list-style-type: none;
}
ul li {
width: 50%;
float: left;
margin: 0;
padding: 0;
}
h1 {
font-size: 3em;
color: #c01818;
margin: 0.5em 0;
padding: 0.25em 0;
border-bottom: 2px #c01818 solid;
}
#container {
width: 920px;
margin: 0 auto;
}
form {
text-align: center;
}
</style>
<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2"></script>
</head>
<body>
<div id="container">
<h1>Find my councillors</h1>
<?php
if ($_POST) {
$postcode = str_replace(" ", "", $_POST['postcode']);
$url = "http://www.lichfielddc.gov.uk/site/custom_scripts/wardsxml.php?postcode=".$postcode;
$ward = get_xml($url);
?>
<h2><?php echo $ward['wards']['ward']['name']['value']; ?> Ward</h2>
<div id="map" style="float: right; width: 500px; height: 500px; position: relative;"></div>
<?php
$councillors = get_xml("http://www.lichfielddc.gov.uk/site/custom_scripts/councillors_xml.php?viewBy=specificward&wardID=".$ward['wards']['ward']['internal-id']['value'] );
if (isset($councillors['members']['member'][0])) {
foreach ($councillors['members']['member'] as $member) {
?>
<h3><?php echo $member['first-name']['value']; ?> <?php echo $member['last-name']['value']; ?></h3>
<p><em><?php echo $member['party']['value']; ?></em></p>
<p><?php echo nl2br($member['address']['value']); ?></p>
<p><strong>Email:</strong> <a href="mailto:<?php echo $member['email']['value']; ?>"><?php echo $member['email']['value']; ?></a></p>
<?php
}
} else {
$member = $councillors['members']['member'];
?>
<h3><?php echo $member['first-name']['value']; ?> <?php echo $member['last-name']['value']; ?></h3>
<p><em><?php echo $member['party']['value']; ?></em></p>
<p><?php echo nl2br($member['address']['value']); ?></p>
<p><strong>Email:</strong> <a href="mailto:<?php echo $member['email']['value']; ?>"><?php echo $member['email']['value']; ?></a></p>
<?php } ?>
<script type="text/javascript">
function GetMap(){
map = new VEMap('map');
map.SetDashboardSize(VEDashboardSize.Small);
map.LoadMap(new VELatLong(51, 0));
map.EnableShapeDisplayThreshold(false);
map.Find(null,'<?php echo $postcode; ?>, UK', null, null, null, null, true, true, null, true, GetCoordinates);
var layer = "http://www.lichfielddc.gov.uk/site/custom_scripts/wardskml.php?ward=<?php echo $ward['wards']['ward']['snac-id']['value']; ?>";
var l = new VEShapeLayer();
var veLayerSpec = new VEShapeSourceSpecification(VEDataType.ImportXML, layer);
map.ImportShapeLayerData(veLayerSpec);
}
function GetCoordinates(layer, resultsArray, places, hasMore, veErrorMessage) {
findPlaceResults = places[0].LatLong;
var myShape = new VEShape(VEShapeType.Pushpin, findPlaceResults);
map.AddShape(myShape);
map.ShowInfoBox(myShape);
}
function addMarker() {
map.Clear();
postcode = document.getElementById('postcode').value;
map.Find(null, postcode + ', UK', null, null, null, null, true, true, null, true, GetCoordinates);
fixmystreet(findPlaceResults);
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}
function addUnLoadEvent(func) {
var oldonunload = window.onunload;
if (typeof window.onunload != 'function') {
window.onunload = func;
} else {
window.onunload = function() {
oldonunload();
func();
}
}
}
addLoadEvent(GetMap);
</script>
<?php } else { ?>
<form action="" method="post">
<h2>My postcode is <input type="text" class="text" name="postcode" onclick="this.value=''" value="WS13 6YY" /> <button class="btn"><span><span>who is my councillor?</span></span></button></h2>
</form>
<?php } ?>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment