在类似信息亭的应用程序中,我试图以设定的时间间隔(在我的情况下为60秒)打印mysql数据库中插入记录的最后60秒。
使用下面的代码,Ajax刷新延迟或是否有任何可能省略打印记录的可能性? 如果是,我有什么办法可以避免这种情况?
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
@media print {
tr.page-break {
display: block;
page-break-before: always;
}
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
function getData(){
$.ajax({
type: 'GET',
url: 'data.php',
success: function(data){
$('#output').html(data);
function isEmpty( el ){
return !$.trim(el.html())
}
if (!isEmpty($('#output'))) {
window.print();
}
}
});
}
getData();
setInterval(function () { getData(); }, 60000); // it will refresh your data every 1 sec
});
</script>
</head>
<body>
<div class="container" id="output"></div>
</body>
</html>
还有data.php
:
<?php
$servername = "localhost";
$username = "root";
$password = "xxx";
$dbname = "testdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select * from orders where time > date_sub(now(), interval 1 minute) ORDER BY id DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>";
// output data of each row
while($row = $result->fetch_assoc()) {
$data1 =$row["id"];
$data2= $row["product"];
$data3= $row["details"];
echo "<tr class='page-break'>";
echo "<td>" . $data1 . "</<td>";
echo "<td>" . $data2 . "</td>";
echo "<td>" . $data3 . "</td>";
echo "</tr>";
}
echo "</table>";
}
$conn->close();
?>
该代码仅用于测试而非生产,因此不要介意php安全性问题(sql注入等)。但是,如果您能以任何方式改进javascript部分,我将不胜感激。
有没有比这更好的解决方案,可以在mysql数据库中打印最近60秒的记录?
答案 0 :(得分:1)
基于@Adder的代码,我最终得到了这一点:
data "external" "download_function" {
program = ["curl", "-L", "-o",
"${path.module}/auth_requests.zip", "${var.github_repo_url}"]
}
和sql
<script>
$(document).ready(function () {
$.ajax({ // Get lastID
type: 'GET',
url: 'lastid.php',
dataType: 'json',
'success': function (data) {
callback(data);
}
});
var return_last;
function callback(response) {
return_last = response;
var lastID = return_last.id;
function getData() {
$.ajax({
type: 'GET',
url: 'data.php',
data: {lastID: lastID},
dataType: 'json',
success: function (data) {
lastID = data[0].id;
console.log(lastID);
$.each(data, function (i, item) {
var $tr = $('<tr class="page-break">').append(
$('<td>').text(item.id),
$('<td>').text(item.name),
$('<td>').text(item.details)
).appendTo('#output');
});
function isEmpty(el) {
return !$.trim(el.html());
}
if (!isEmpty($('#output'))) {
window.print();
}
}
});
}
getData();
setInterval(function () {
$("tr").addClass("no-print");
getData();
}, 60000); // it will refresh your data every 1 minute
}
});
</script>
答案 1 :(得分:0)
我编写了以下代码作为示例,以在SQL查询中使用lastTime
变量,以使SQL查询在最后一个查询结束的地方继续进行。
$(document).ready(function(){
var lastTime = 0;
function getData(){
$.ajax({
type: 'GET',
url: 'data.php',
data: {lastTime: lastTime},
dataType: 'json',
success: function(data){
for(var i = 0; i<data.length; ++i) {
var row = data[i];
printRow('#output', row);
lastTime = row.time;
}
}
});
}
function printRow(where, row) {
//build html
var html = '';
html+= '<div class="row">';
... // TODO
html+= '</div>';
$(where).append(html);
}
getData();
setInterval(function () { getData(); }, 60000); // it will refresh your data every 1 minute
});