Last active
February 17, 2016 13:52
-
-
Save Daniel-Griffiths/c75181639531e0f4bb8e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
|-------------------------------------------------------------------------- | |
| Broadband Speed Class | |
|-------------------------------------------------------------------------- | |
| | |
| This class allows you to parse a CSV file and return the average UK | |
| broadband speed. | |
| | |
| https://data.gov.uk/dataset/broadband-coverage | |
| | |
*/ | |
class broadband | |
{ | |
/** | |
* search the array data and return the broadband | |
* speed if it matches the serach term | |
* | |
* @param string $searchTerm | |
* @param string $csvFile | |
* @return string | |
*/ | |
public static function searchData($searchTerm, $csvFile) | |
{ | |
$csvData = self::csv2array($csvFile); | |
/* loop through every area */ | |
foreach($csvData as $key=>$value){ | |
$area = $value[0]; | |
$speed = $value[1]; | |
/* check if the current area | |
matches our search term */ | |
if(stripos($area, $searchTerm) == true){ | |
return $speed; | |
} | |
} | |
return false; | |
} | |
/** | |
* return the csv file as an array | |
* | |
* @param string $csvFile | |
* @return array | |
*/ | |
private static function csv2array($csvFile) | |
{ | |
if(file_exists($csvFile)) | |
{ | |
return array_map('str_getcsv', file($csvFile)); | |
} | |
return false; | |
} | |
} | |
?> | |
<form method="post"> | |
<input type="text" name="area" placeholder="area"> | |
<input type="submit"> | |
</form> | |
<?php | |
if(isset($_POST['area'])) | |
echo $_POST['area'] . ' - average speed is | |
' . broadband::searchData($_POST['area'],'data.csv') . 'mbps'; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment