duanbiao4035 2016-11-12 12:29
浏览 54
已采纳

根据对Mysql Records计数的更改执行脚本

How do I execute a script based on a change to the total count of records in a mysql table. div1 echoes the amount of records in a table just fine. Using the .load ajax request, the div is constantly refreshed every 0,5 second so it'll always know how many records there are almost real-time.

 <div class="div1"> 
            <?php $row_cnt = mysqli_num_rows($result);   
            echo "table has $row_cnt records" ; ?>                  
 </div>

 <script>
    function refreshdiv(){
        $('.div1').load('backendform.php',function () {
             $(this).unwrap();
        });
    }
    refreshdiv();
    setInterval(function(){
        refreshdiv();
    }, 500);
</script>

Here I'm trying to figure out how to call a JS function when the count has changed within the last minute (or second, doesn't matter). So if $row_cnt has for example changed from outputting 5 to 6, how do I trigger the execution for this statement?

<?php
//if record was added in last minute
if($row_cnt...)
echo '<script type="text/javascript">',
     'notification();',
     '</script>'
;?>
<script>

I suppose what also could work is if I could tell if a record has been added to the Mysql table within the last few seconds. Each record has a timestamp. But I don't even know if this can be done because I found no similar questions on SO.

  • 写回答

1条回答 默认 最新

  • douzuqin3467 2016-11-12 13:45
    关注

    You can set data for #div1 like data-row

    <?php $row_cnt = mysqli_num_rows( $result ); ?>
    <div class="div1" data-row="<?php echo $row_cnt ?>">
        <?php echo "table has $row_cnt records"; ?>
    </div>
    

    put notification function in setInterval and use #div1 data-row to compare:

    <script>
        function refreshdiv() {
            $('.div1').load('backendform.php', function () {
                $(this).unwrap();
            });
        }
        refreshdiv();
        function notification() {
            var row = $('.div1').attr('data-row');
            if (row == 2) {
                //do something
            }
            else{
                //do something
            }
        }
        setInterval(function () {
            refreshdiv();
            notification();
        }, 500);
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看