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:
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:
- 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!!
- 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>