How to maintain a constant connection to the database for updating data without the need to refresh

Asked

Viewed 79 times

0

I have an application that is working with Mysql and PHP databases. There is data in this comic that are updated frequently because it is a vote, but this vote takes place on an external site and on my site only displays the rank with the total votes.

To make this rank more dynamic, I wanted to maintain a constant connection with db so that the rank updates whenever a new vote is counted in the database, without the need for a refresh.

$rank = $_POST["rank"] // Nesse caso usarei o rank de músicas.
$query = mysqli_query($conn, "SELECT * FROM $rank ORDER BY votos LIMIT 0, 10");

A very brief example of how data display occurs.

I use an echo to return to the site the html that is generated in the php page itself and the jquery displays.

1 answer

0

For this use the function setTimeout javascript to call the request.

Example

// int t: difine o tempo que chamara a função novamente.
function atualizaRank(t) {
  $.get( "rank.php", function( data ) {
    $( "#resultado" ).html( data );
    setTimeout(function(){ atualizaRank(t) }, t);
  });
}

// 10000 milessegundo = 10 segundos
atualizaRank(10000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="resultado"></div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.