0

I have a table in a database with some values referring to humidity, temperature, brightness and noise of a specific room in a specific hour (in a specific date). I want to put them on a html table like this:

example html table

The problem is that my html table has to display only the values of a precise day, so at the end of the day the html table has to have 96 values (from 00:00 to 23:45) and in the next day it has to be emptied. I realized that there might be two ways to solve it:

  1. At the end of the day, the rows must be deleted, so with the two loops (one to create the rows and the other for the single cells), the table for the next day can be re-created. I don't know how to delete the html rows!!
  2. At the end of the day, with a "pointer" it is possible to return to the first row and to change cell's contents.

In both cases, I need a "pointer" that allows to return in the beginning of the table. Here is my code but there are some bugs. Please also let me know if you have different ideas.

<?php
// Ignore user aborts and allow the script
// to run forever
ignore_user_abort(true);
?>

<html>
    <head>
        <meta charset="utf-8">
        <script>
            function waiting() 
            {
                //wait 15 minutes (900 seconds) until the insertion of new data 
                setTimeout(function() {alert("New line added!"); }, 900);
            }
        </script>
        <title> Benvenuto </title>
    </head>
    <body>
        <table style="width:100%" border='0'>
            <tr>
                <td valign="top">
                    <input type="date" value="<?php echo date("Y-m-d");?>">
                </td>
            </tr>
        </table>

        <table border='0' style="width:100%">
            <tr>
                <td>
                    <b> Hour </b>
                </td>
                <td>
                    <b> ID room </b>
                </td>
                <td>
                    <b> Humidity </b>
                </td>
                <td>
                    <b> Temperature </b>
                </td>
                <td>
                    <b> Brightness </b>
                </td>
                <td>
                    <b> Noise </b>
                </td>
            </tr>
        </table>
        <?php
            mysql_connect(" localhost", "root", "root");
            mysql_select_db("my_db");
            $date = date("d/m/Y");
            $hour_now = date('H:i', strtotime('00:00'));
            $hour_end = date('H:i', strtotime('23:45'));
            $row = 96; //Dynamic number for rows
            $col = 6; // Dynamic number for columns
            do 
            {
                echo "<table>";
                //rows loop
                for($i=0;$i<$row;$i++)
                {
                    echo "<tr>";
                    //cells loop
                    for($j=0;$j<$col;$j++)
                    {
                        echo "<td>";
                            //hour
                            $hour_on_table = mysql_query("SELECT hour FROM DATA WHERE hour='$hour_now' AND date='$date'");
                            echo mysql_result($hour_on_table, 0);
                        echo "</td>";
                        echo "<td>";
                            //id_room
                            $id_room = mysql_query("SELECT id_room FROM DATA WHERE hour='$hour_now' AND date='$date'");
                            echo mysql_result($id_room, 0);
                        echo "</td>";
                        echo "<td>";
                            //humidity
                            $humidity = mysql_query("SELECT humidity FROM DATA WHERE hour='$hour_now' AND date='$date'");
                            echo mysql_result($humidity, 0);
                        echo "</td>";
                        echo "<td>";
                            //temperature
                            $temperature = mysql_query("SELECT temperature FROM DATA WHERE hour='$hour_now' AND date='$date'");
                            echo mysql_result($temperature, 0);
                        echo "</td>";
                        echo "<td>";
                            //brightness
                            $brightness = mysql_query("SELECT brightness FROM DATA WHERE hour='$hour_now' AND date='$date'");
                            echo mysql_result($brightness, 0);
                        echo "</td>";
                        echo "<td>";
                            //noise
                            $noise = mysql_query("SELECT noise FROM DATA WHERE hour='$hour_now' AND date='$date'");
                            echo mysql_result($noise, 0);
                        echo "</td>";

                        echo '<script type="text/javascript"> attesa(); </script>';
                        $hour_now = date('H:i', strtotime($hour_now) +900);    //add 15 min
                    }
                    echo "</tr>";
                }
                echo "<table>";
            }
        ?>
    </body>
</html>
Nishi
  • 614
  • 4
  • 18
Besjan Veizi
  • 1
  • 1
  • 1
  • 4
  • 2
    on an unrelated note... it would be much better to query the db once and get everything you need and then use the data. get noise, humidity, temperature etc. in one query and then print values. – hummingBird Feb 10 '16 at 13:39
  • 2
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Feb 10 '16 at 13:40
  • 1
    I believe that you can do what you want to do with JavaScript, but in general the questions are pretty broad. – Jay Blanchard Feb 10 '16 at 13:41
  • I think your best solution would be to write a better query that only selects the records for the selected date. You do not use the date at all in your query, and you are doing multiple queries to get each field from a record. Why not get all the relevant fields from all the relevant records all in one well written query. – Dustin Poissant Feb 10 '16 at 13:42
  • agreed with jay... there are ways to do this. and you didn't explain how data gets written in database. you could be inserting them with another script and using this page just for reading data for current day from the db... again, we don't know much details. – hummingBird Feb 10 '16 at 13:43
  • there is a device that control temperature, humidity, noise and brightness and the values are saved in the database – Besjan Veizi Feb 10 '16 at 13:48

2 Answers2

0

I think your best solution would be to write a better query that selects all fields of all records for a day. And then just iterate through the results and echo it.

<?php
  if($_REQUEST['date'])
    $date = new DateTime($_REQUEST['date']);
  else
    $date = new DateTime("now");
  $db = mysqli_connect('localhost', 'root', 'root', 'my_db');
  // Check for SQL connection errors here
  $records = mysqli_query($db, "SELECT * FROM DATA WHERE `date`='".$date->format('m/d/Y')."' ORDER BY `hour`");
?>
<html>
<head>
  
</head>
<body>
  <form>
    <input type='date' name='date' value='<?php echo $date->format('m/d/Y'); ?>' />
    <button type='submit'>Update</button>
  </form>
  <table>
    <thead><tr>
      <td>Hour</td>
      <td>ID Room</td>
      <td>Humidity</td>
      <td>Temperature</td>
      <td>Brightness<td>
      <td>Noise<td>
    </tr></head>
    <tbody>
      <?php
        while($record = mysqli_fetch_assoc($records)){
          echo "<tr>";
            echo "<td>{$record['hour']}</td>";
            echo "<td>{$record['ID_room']}</td>";
            echo "<td>{$record['humidity']}</td>";
            echo "<td>{$record['temperature']}</td>";
            echo "<td>{$record['brightness']}</td>";
            echo "<td>{$record['noise']}</td>";
          echo "</td>";
        }
      ?>
    </tbody>
  </table>
</body>
</html>
Dustin Poissant
  • 3,201
  • 1
  • 20
  • 32
0

No. You sent it out to the browser. You have to use javascript to delete it.

But you should probably not make your php script run forever. Something will time out sooner or later between your server and your browser.

You should use an ajax call in setTimeout to refresh the data. E.g. you could use jquery.Ajax, parse the response with jQuery.parseHTML, cut out the table from it, and replace the old table with replaceWith

Something like this (untested):

$.ajax(url).done(function(data) {
    $('table:first') // this finds the first table element in the html
        .replaceWith(
            $.parseHTML(response).find('table:first') // cut out the table element from the response
        ); // replace the table in the html with the one in the ajax response
})
user1431317
  • 2,674
  • 1
  • 21
  • 18