Skip to content

Instantly share code, notes, and snippets.

@Daniel-Griffiths
Last active February 17, 2016 13: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 Daniel-Griffiths/c75181639531e0f4bb8e to your computer and use it in GitHub Desktop.
Save Daniel-Griffiths/c75181639531e0f4bb8e to your computer and use it in GitHub Desktop.
<?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