Covid 19 Tracker Web App using PHP
In this article, we will see how to create a web application for tracking the Covid19 using PHP. Our Coivd19 Tracker app will give the latest information for the States and Union Territories of India about the following things.
- Number of Active Cases of Covid19.
- Number of Confirmed Cases of Covid19.
- Number of Recovered Cases of Covid19.
- Number of Deaths from Covid19.
The Source to fetch all the above data is https://api.covid19india.org/data.json which actually is an API that returns the data in the form of JSON file. The main idea is to use the Associative Arrays in PHP so that we can get the required information from the JSON file.
After going through https://api.covid19india.org/data.json, we will get state-wise data in form of an associative array which is as follows:
So we will store the data obtained from the JSON file in a variable say $data that can be done by using file_get_contents() method, and we also need to decode the JSON file by using json_decode() method.
The file_get_contents() method is used to read the content of the file storing in a variable. As we get the data in JSON format, we first convert it into an array. For that, we are using json_decode() function. The json_decode() function is used to decode or convert a JSON object to a PHP object. Now we can easily parse the array object using the array operator and show the details to the user.
Below is PHP Code snippet for doing this.
$data=file_get_contents('https://api.covid19india.org/data.json'); $coronalive =json_decode($data,true);
Now after we decode the JSON file, we will display the required information in form of a Table in which we will make Name of State, Number of Active cases, Confirmed cases, Recovered Cases and Deaths as table headers and for each state, we will fetch data by using associative arrays in PHP in which every state is uniquely determined by an index.
To implement this, we can make a PHP file say index.php which would be as follows:
Live Demonstration of Project: https://covid-ap.herokuapp.com/index.php
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML course.