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 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用