So I am trying to have a countdown for each value from a database. The value is in seconds and i am wanting to countdown from (let's say 100) to 0 and then display Finished
However, when I tried this my results got all muddled up and were not counting down in intervals of 1 second. If there were 3 results then it counted down in intervals of 3. I apologise in advance for my lack of knowledge regarding this.
This is my code for it all
<?php
$curtime = date("Y-m-d h:i:s");
$curtime = strtotime("$curtime UTC");
if (logged_in() === true){
$results = $dbc->query($sql);
while ($attacks = mysqli_fetch_array($results)){
$attime = $attacks['date'] - ($attacks['length'] + $curtime);
?>
<tr>
<td class="text-left"><?php echo $attacks['attack_ip'];?></td>
<td class="text-left"><?php echo $attacks['port'];?></td>
<td class="text-left"><?php echo $attacks['method'];?></td>
//////This is where the countdown is//////
<td id="<?php echo $attacks['attack_id'];?>" class="text-left"><?php echo $attime;?></td>
<td><button type="button" class="btn btn-effect-ripple btn-square btn-danger">Stop</button></td>
</tr>
<script type="text/javascript">
var sec = <?php echo $attime;?>;
var timer = setInterval(function() {
$('td#<?php echo $attacks['attack_id'];?>').text(sec--);
if (sec == -1) {
$('td#<?php echo $attacks['attack_id'];?>');
clearInterval(timer);
}
if (sec == -1) {
$('td#<?php echo $attacks['attack_id'];?>').html("<b>Finished</b>");
clearInterval(timer);
}
}, 1000);
</script>
<?php
} //End WHILE
}//END IF
?>