Open In App

Covid 19 Tracker Web App using PHP

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to create a web application for tracking the Covid19 using PHP. Our Covid19 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:

The output of JSON file

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:

PHP




<!DOCTYPE html>
<html>
  
<head>
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <title>Covid19 Report</title>
  
    <style type="text/css">
        .header {
            background-image: url('cool-background.png');
            width: 40%;
            font-family: 'Niconne';
            box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 
                    0 6px 20px 0 rgba(0, 0, 0, 0.19);
            border-radius: 20px;
            background-size: auto;
        }
  
        .middle {
            margin-top: 60px;
            opacity: 1.0;
            border-radius: 20px;
            background-color: #f2f2f2;
            background-image: url('cool-background3.png');
            background-size: auto;
            padding: 20px;
        }
  
        th {
            padding-top: 12px;
            padding-bottom: 12px;
            text-align: left;
            background-color: #4CAF50;
            color: white;
        }
  
        td,
        th {
            border: 1px solid #ddd;
            padding: 8px;
        }
  
        tr:nth-child(even) {
            background-color: #f2f2f2;
        }
  
        tr:hover {
            background-color: #ddd;
        }
    </style>
</head>
  
<body>
    <center>
        <div class="header">
            <h1>Covid19 Tracker</h1>
        </div>
  
        <div class="middle">
            <h2>
                Latest Updates of Covid19 
                about States and Union 
                Territories of India
            </h2>
        </div>
  
        <div style="overflow-x:auto;">
            <table border="1px ">
                <?php
                $data=file_get_contents(
  
    $coronalive =json_decode($data,true);
  
    // echo $coronalive['statewise'][1]['state'];
    $satecount = count($coronalive['statewise']);
                ?>
                <tr>
                    <th>State</th>
                    <th>Last Updated Date Time</th>
                    <th>Confirmed Cases</th>
                    <th>Active Cases</th>
                    <th>Recovered Cases</th>
                    <th>Death Cases</th>
                </tr>
                <?php   
                $i = 1;
                while($i < 38) { 
                ?>
                <tr>
                    <td>
<?php echo $coronalive['statewise'][$i]['state'] ?>
                    </td>
  
                    <td>
<?php echo $coronalive['statewise'][$i]['lastupdatedtime'] ?>
                    </td>>
  
                    <td>
<?php echo $coronalive['statewise'][$i]['confirmed'] ?>
                    </td>
  
                    <td>
<?php echo $coronalive['statewise'][$i]['active'] ?>
                    </td>
  
                    <td>
<?php echo  $coronalive['statewise'][$i]['recovered'] ?>
                    </td>
  
                    <td>
<?php echo $coronalive['statewise'][$i]['deaths'] ?>
                    </td>
                </tr>
                <?php  $i++;
                }
                ?>
            </table>
        </div>
    </center>
</body>
  
</html>


Output:



Last Updated : 07 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads