简体   繁体   English

刷新div的每次新记录插入mysql数据库

[英]Refreshing div's everytime new record is inserted in mysql database

I'm working on SCADA system on my webpage and want to refresh certain div's with gif's only when any of 2 tables from mysql database change. 我正在我的网页上使用SCADA系统,并且仅当mysql数据库的2个表中的任何一个发生更改时,才希望使用gif刷新某些div。

And also i want to do it everytime the change occur (new record in table). 而且我也想在每次更改发生时执行此操作(表中的新记录)。 When these 2 tables stay the same no action should be made (no new records). 当这两个表保持不变时,不应执行任何操作(没有新记录)。

Whole webpage is php/html and this is my first approach to javascript/ajax. 整个网页都是php / html,这是我使用javascript / ajax的第一种方法。 I've tried using: 我试过使用:

  setInterval($('. examplediv).load(location.href + ' .examplediv), 1000);

but it doesn't suit my needs. 但这不符合我的需求。 It makes my graphics flicker with each refresh when they should be static. 当我的图形应该是静态的时,它会随着每次刷新而闪烁。 To simplify: I want to reload them only if state in table changes from a->b so i swap a.gif with b.gif. 为简化起见:我只想在表中的状态从a-> b更改时重新加载它们,所以我将a.gif与b.gif交换。

I can detect change in mysql tables by: record ID, timestamp or table checksum but struggle hard with javascript. 我可以通过以下方式检测mysql表中的更改:记录ID,时间戳或表校验和,但很难使用javascript。

<div class="refresh"> // div i want to refresh
  <h3>PIR</h3>        // sensor name
  <p>Online: <?echo $aonline;?></p>  // echoing result of query if  sensor is online
  <p>State: <?echo $pirstan;?></p>      // echoing current state of  sensor
  <p>Last changed: <?echo $rowpir['Czas'];?></p>  //echoing timestamp of previous state of sensor
   <img class="center" src="pir.gif" alt="Kontrakton"> // showing image related to current sensor state (for example walking person to PIR sensor)
  </div>

Any help with problem above would be apprieciated. 以上问题的任何帮助将不胜感激。 Especially example code with comments so i understand the matter. 特别是带有注释的示例代码,这样我就明白了。 Other approach to the problem is also welcome, maybe I'm short-sighted. 也欢迎使用其他解决问题的方法,也许我是短视的。

You can use a websocket or socket.io to solve your problem example: 您可以使用websocket或socket.io解决您的问题示例:

When your client get the info You can straight make an ajax call 当您的客户获得信息时,您可以直接拨打Ajax电话

socket.on('notification', function(){
  ajaxFunction();
});

If you dont want to you this method. 如果您不想使用此方法。 p/s This is just a suggestion. p / s这只是一个建议。 Do validation fetch row data old and compare row data new in your backend. 进行验证以获取旧的行数据,并在后端中比较新的行数据。

Via js 通过js

<script>

var old_count = 0;

setInterval(function(){    
    $.ajax({
        type : "POST",
        url : "test.php",
        success : function(data){
            if (data > old_count) {
                alert('new record on i_case');
                old_count = data;
            }
        }
    });
},1000);

</script>

For those interested in future, final code thanks to: Fiido93 对于那些对未来感兴趣的人,请提供以下最终代码:Fiido93

<script type="text/javascript">
var old_count = 0;
setInterval(function(){    
    $.ajax({
        type : "POST",
        url : "test.php",
        success : function(data){
            if (data > old_count) {
                   $('.divname' ).load(window.location.href + ' .divname');
               old_count = data;
           }
        }
    });
},1000);

and test.php 和test.php

$result = mysqli_query($conn,"mysqlquery");
while($row = mysqli_fetch_array($result))
{
    $printme= $row[0];   // i only echo row 0 here !!
} 
echo "$printme";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM