从SQL数据库动态收集数据的最佳方法是什么

时间:2022-09-15 21:08:19

So currently I have a system that pulls data from an SQl database and displays the details in separate counts.

所以目前我有一个系统从SQl数据库中提取数据并以不同的计数显示细节。

For Example, Total Tickets, Tickets with Support, Tickets with customer etc.

例如,门票总数,支持门票,客户门票等

The only time this data get update is when the user reload the page or navigates to another part of the system.

此数据更新的唯一时间是用户重新加载页面或导航到系统的另一部分。

I am looking to revamp the whole system to that the data is always valid instead of users needing to reload the page.

我希望改进整个系统,使数据始终有效,而不是需要重新加载页面的用户。

I have been looking into JQuery and AJAX however all the information I have been finding still requires user input.

我一直在研究JQuery和AJAX,但是我发现的所有信息仍然需要用户输入。

So I would some advise or links on how to setup a system that requires no user input and instead pulls the data every X seconds and updates the page without refreshing it.

所以我会建议或链接如何设置一个不需要用户输入的系统,而是每X秒拉一次数据并更新页面而不刷新它。

I also want to then be able to expand this functionality to display in-page alerts when new tickets are logged etc.

我还希望能够扩展此功能,以便在记录新故障单时显示页内警报等。

1 个解决方案

#1


2  

" all the information I have been finding still requires user input."

“我发现的所有信息仍然需要用户输入。”

...not sure what you've been reading, but no, you can set a timeout or interval to make a fixed AJAX call on a regular schedule in order to fetch the latest data, all without user intervention.

...不确定你一直在读什么,但不,你可以设置一个超时或间隔来定期进行固定的AJAX调用,以便获取最新数据,所有这些都无需用户干预。

Here's a demo. In this case it appends the same data from a static source every time, but if your source returns database data which is being updated in the background, then you might want to replace what's there already rather than appending. It's a small detail, but worth clarifying.

这是一个演示。在这种情况下,它每次都会从静态源追加相同的数据,但如果您的源返回在后台更新的数据库数据,那么您可能希望替换已存在的数据而不是追加。这是一个小细节,但值得澄清。

//when called, this will make an AJAX call and append data from the response to the "content" div
function getData() {
  $.ajax({
    method: "GET",
    url: "https://api.myjson.com/bins/df42s",
    dataType: "json"
  }).done(function(response) {
    $("#content").append("<br/>" + response.name);
  });
}

//initial fetch of the data
getData();

//then set the same function to run at 5 second intervals:
var interval = setInterval(getData, 5000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content">

Be careful not to set the interval to be too frequent, otherwise you might overload your server, and/or find that one AJAX call has not completed before the next one starts. Neither of these scenarios is very desirable.

注意不要将间隔设置得太频繁,否则可能会使服务器过载,和/或在下一个AJAX调用启动之前发现一个AJAX调用尚未完成。这些场景都不是非常理想的。

More info about setInterval: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval

有关setInterval的更多信息:https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval

#1


2  

" all the information I have been finding still requires user input."

“我发现的所有信息仍然需要用户输入。”

...not sure what you've been reading, but no, you can set a timeout or interval to make a fixed AJAX call on a regular schedule in order to fetch the latest data, all without user intervention.

...不确定你一直在读什么,但不,你可以设置一个超时或间隔来定期进行固定的AJAX调用,以便获取最新数据,所有这些都无需用户干预。

Here's a demo. In this case it appends the same data from a static source every time, but if your source returns database data which is being updated in the background, then you might want to replace what's there already rather than appending. It's a small detail, but worth clarifying.

这是一个演示。在这种情况下,它每次都会从静态源追加相同的数据,但如果您的源返回在后台更新的数据库数据,那么您可能希望替换已存在的数据而不是追加。这是一个小细节,但值得澄清。

//when called, this will make an AJAX call and append data from the response to the "content" div
function getData() {
  $.ajax({
    method: "GET",
    url: "https://api.myjson.com/bins/df42s",
    dataType: "json"
  }).done(function(response) {
    $("#content").append("<br/>" + response.name);
  });
}

//initial fetch of the data
getData();

//then set the same function to run at 5 second intervals:
var interval = setInterval(getData, 5000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content">

Be careful not to set the interval to be too frequent, otherwise you might overload your server, and/or find that one AJAX call has not completed before the next one starts. Neither of these scenarios is very desirable.

注意不要将间隔设置得太频繁,否则可能会使服务器过载,和/或在下一个AJAX调用启动之前发现一个AJAX调用尚未完成。这些场景都不是非常理想的。

More info about setInterval: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval

有关setInterval的更多信息:https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval