CakePHP 4 Covid-19 Tracker Module

CakePHP 4 Covid-19 Tracker Module

This tutorial will demonstrate how to populate data from JSON and CSV format from online sources. Technically the module is not limited to the CakePHP where it also can be applied to others website or web application with PHP support. The Covid-19 module tracker will present Malaysia data 1) new case; 2) new death; 3) active case; 4) total recovered; 5) total case; 6) total death; 7) total people vaccinated; 8) total fully vaccinated and; 9) total vaccination. It also includes the worldwide data: total worldwide recovered; total worldwide case and total worldwide death.


The data source used in this tutorial is from DataFlow Kit and Our World in Data (OWID). Taking the advantage of the element in CakePHP. Element helps you repeat parts of your website that need to be reused.  To create a module, create a file named module_covid.php in …templates/element. 

Load the data source
Open the module_covid.php file and load the data source. There are two data sources used in this module to demonstrate how to read data from JSON and CSV format. The data from the DataFlow Kit is in JSON format and the data source from OWID is in CSV format. The data will be updated daily (GMT). The following codes is to include the aforementioned data source and print all values.

<?php
//Malaysia Covid-19 Data by DataFlow Kit
$dataMalaysia = file_get_contents('https://covid-19.dataflowkit.com/v1/Malaysia');
$decodedMalaysia = json_decode($dataMalaysia);
//Print all JSON Malaysia data
echo '<pre>'; 
echo json_encode(json_decode($dataMalaysia), JSON_PRETTY_PRINT); 
echo '</pre>'; 


//World Covid-19 Data by DataFlow Kit
$dataWorld = file_get_contents('https://covid-19.dataflowkit.com/v1/world');
$decodedWorld = json_decode($dataWorld);
//Print all JSON worldwide data
echo '<pre>'; 
echo json_encode(json_decode($dataWorld), JSON_PRETTY_PRINT); 
echo '</pre>'; 

//Malaysia Covid-19 Vaccination Data by OWID
$rows = file('https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/country_data/Malaysia.csv');
$last_row = array_pop($rows);
$dataVaccine = str_getcsv($last_row);
//Print all JSON Malaysia Vaccination data
for($i = 1; $i < count($dataVaccine); $i++) {
    $line = explode(',', $dataVaccine[$i]);
    for($j = 0; $j < count($line); $j++) {
        $array[$i][$j + 1] = $line[$j];
    }
}
echo '<pre>';
print_r($array);
echo '</pre>';
?>

 

Now, let's read and print the specific JSON and CSV data. Below is the code that contains how to read and print the specific data.

<?php
//Malaysia Covid-19 Data by DataFlow Kit
$dataMalaysia = file_get_contents('https://covid-19.dataflowkit.com/v1/Malaysia');
$decodedMalaysia = json_decode($dataMalaysia);
//Print all JSON data
echo '<pre>'; 
echo json_encode(json_decode($dataMalaysia), JSON_PRETTY_PRINT); 
echo '</pre>'; 
//Print specific data from JSON
echo 'Country: ' . $decodedMalaysia->Country_text; //print JSON data without white space in name
echo '<br>Last Updated: ' . $decodedMalaysia->{'Last Update'}; //print JSON data with white space in name
echo '<br>New Case:' . $decodedMalaysia->{'New Cases_text'};
echo '<br>New Death:' .  $decodedMalaysia->{'New Deaths_text'};
echo '<br>Active Case:' .  $decodedMalaysia->{'Active Cases_text'};
echo '<br>Total Recovered:' .  $decodedMalaysia->{'Total Recovered_text'};
echo '<br>Total Case:' .  $decodedMalaysia->{'Total Cases_text'};
echo '<br>Total Death:' .  $decodedMalaysia->{'Total Deaths_text'};

//World Covid-19 Data by DataFlow Kit
$dataWorld = file_get_contents('https://covid-19.dataflowkit.com/v1/world');
$decodedWorld = json_decode($dataWorld);
//Print world data. Same as print Malaysia data above
echo '<br>Total Worldwide Recovered: ' . $decodedWorld->{'Total Recovered_text'};
echo '<br>Total Worldwide Cases: ' . $decodedWorld->{'Total Cases_text'};
echo '<br>Total Worldwide Death: ' . $decodedWorld->{'Total Deaths_text'};

//Malaysia Covid-19 Vaccination Data by OWID
$rows = file('https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/country_data/Malaysia.csv');
$last_row = array_pop($rows);
$dataVaccine = str_getcsv($last_row);

for($i = 1; $i < count($dataVaccine); $i++) {
    $line = explode(',', $dataVaccine[$i]);
    for($j = 0; $j < count($line); $j++) {
        $array[$i][$j + 1] = $line[$j];
    }
}
echo '<pre>';
print_r($array);
echo '</pre>';

$dateTime = new DateTime($array[1][1]);  //convert to dateTime format
echo $dateTime->format('d F Y (l)'); //Print last update (1,1) in dateTime format
echo '<br> People Vaccinated: ' . number_format($array[5][1], 0, '.', ','); //Print array 5,1 total people vaccinated with PHP number formatting
echo '<br> People Fully Vaccinated: ' . number_format($array[6][1], 0, '.', ',');
echo '<br> Total Vaccination: ' . number_format($array[4][1], 0, '.', ',');
?>


Now you should be able to access the JSON data and CSV data. In JSON, if the data name has white spacing, {curlyBracket} is required. To call the element, the following code is used. 

<?= $this->element('module_covid'); ?>


With the available data, you can simply add your template or design to the module eg:

  


That's all, happy design and coding :)