You may be making use of AJAX for this. I’ll leave an example with jQuery which is a javascript framework that makes this part easier.
Myfile.php
<?php
include("connection.php");
$query = "SELECT registroNovo FROM registros WHERE BLregistros=0 AND Usuario='$usuario'";
$result_1 = mysqli_query($con, $query);
$num_notificacoes = mysqli_num_rows($result_1);
if($num_notificacoes != 0){
echo $num_notificacoes;
} else {
echo '0';
}
Myfile.php
<div class="w3-right">
<h3 class="notification-number">-</h3>
</div>
<script>
var atualizaNotificacao = function (tempoParaChecarNovamenteEmSegundos) {
$.ajax({
url: 'MeuArquivo.php',
type: 'GET',
beforeSend: function () {
// loading..
$('.notification-number').html('-');
},
success: function (resultado) {
$('.notification-number').html(resultado);
setTimeout(function() { atualizaNotificacao(tempoParaChecarNovamenteEmSegundos); }, tempoParaChecarNovamenteEmSegundos * 1000);
},
error: function () {
$('.notification-number').html('error');
setTimeout(function() { atualizaNotificacao(tempoParaChecarNovamenteEmSegundos); }, tempoParaChecarNovamenteEmSegundos * 1000);
}
});
}
// inicializando contador de tempo para atualizacao de 2 segundos...
atualizaNotificacao(2);
</script>
ATTENTION: I imagine you have jQuery in your application, if you don’t have include the script below before starting the script tag Myfile.php.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
Hello, I did it this way, but it keeps showing the value 0, and has record to show in the database
– Joana
It worked yes your code, is giving problem with my user, I will solve here, mto msm thanks for helping me!
– Joana