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.
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>