我在数据库中有一个表,其中一些值指特定时间内(特定日期)特定房间的湿度,温度,亮度和噪音。我想把它们放在像这样的html表中:
问题是我的html表只需要显示精确日期的值,所以在一天结束时,html表必须有96个值(从00:00到23:45)并在下一个它必须被清空。 我意识到可能有两种方法可以解决它:
在这两种情况下,我都需要一个"指针"允许在表格的开头返回。 这是我的代码,但有一些错误。如果您有不同的想法,也请告诉我。
<?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>
答案 0 :(得分:0)
我认为您最好的解决方案是编写一个更好的查询,选择一天内所有记录的所有字段。然后只是迭代结果并回应它。
<?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>
&#13;
答案 1 :(得分:0)
没有。您已将其发送到浏览器。你必须使用javascript删除它。
但你可能不应该让你的PHP脚本永远运行。服务器和浏览器之间的某些东西迟早会超时。
您应该在setTimeout中使用ajax调用来刷新数据。例如。您可以使用jquery.Ajax,使用jQuery.parseHTML解析回复,从中删除表格,并将旧表格替换为replaceWith
像这样(未经测试):
$.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
})