0

I want to set a timer for one column of a table, the column contain time and I want to set a timer for this column. Each cell of this column has a specific time, if the time is 1 hour before 20 minutes of that hour it should display a popup message which the time is going to finish.

Below is my table:

column name: Time Difference

<body>
    <div class="card-body">
        <table class="table">
            <thead>
            <tr>
                <th>IdCard</th>
                <th>Name</th>
                <th>Company</th>
                <th>Floors</th>
                <th>Arrival</th>
                <th>Departure</th>
                <th>Time Difference</th>
            </tr>
            </thead>
<?php
include('includes/dbconnection.php');
$sql="SELECT *,
            TIME_FORMAT(arrival, '%h:%i %p') AS arrival,
            TIME_FORMAT(departure, '%h:%i %p') AS departure,
            TIME_FORMAT(timediff(departure, arrival), '%H:%i') AS difftime
        FROM master where Status = 'Pending'";

$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);

$cnt=1;
if($query->rowCount() > 0) {
    foreach($results as $row) {
?>   

            <tr>
                <!-- <td><?php echo htmlentities($cnt);?></td>-->
                <td><?php  echo htmlentities($row->idcard);?></td>
                <td><?php  echo htmlentities($row->name);?></td>
                <td><?php  echo htmlentities($row->company);?></td>
                <td><?php  echo htmlentities($row->floors);?></td>
                <td><?php  echo htmlentities($row->arrival);?></td>
                <td><?php  echo htmlentities($row->departure);?></td>
                <td><span style="color: red;"><?php  echo htmlentities($row->difftime);?></span></td>
            </tr>
<?php 
        $cnt=$cnt+1;
    }
} 
?>   

        </table>
    </div>                                  
</body>

Table Image

Eliana
  • 1
  • 1
  • Why 2 `` tags following each other – RiggsFolly Jun 09 '20 at 13:12
  • Good code indentation would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](https://www.php-fig.org/psr/psr-12/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Jun 09 '20 at 13:18
  • @RiggsFolly edited – Eliana Jun 09 '20 at 13:18

1 Answers1

0

Eliana,

See these answers how to refresh PHP page by browser:

  1. Refresh a page using PHP
  2. PHP-script refresh it self and restart execution-time

After the page refresh your current code will then display the new time difference in the last column 'Time Difference'.

If you still want to show pop up then you need to use some Javascript to display the pop after page is loaded, e.g.:

<body onload="showArrivingSoon()">

<!-- Your code ... -->

<script>
    <?php
        // Iterate through rows to produce string $arrivingSoonAsStringDelimitedByNewLine ...
    ?>

    // Here the prepared string is printed to the page and initialize JS variable
    var arrivingSoon = '<?php echo $arrivingSoonAsStringDelimitedByNewLine ?>';

    function showArrivingSoon(arrivingSoon)
    {
        alert('Arriving soon:\n' + arrivingSoon);
    }
</script>
</body>

The iteration, calculations and checks could be done in Javascript as well. To do that it's easier first to pass data rows as JSON from PHP to Javascript variable, then iterate.