I am making a (kinda) dashboard for managing Minecraft servers. What I am currently trying to do is printing how many players are online (across all servers) AND refreshing the value every 3 seconds.
What works - showing the total amount of players - once only; refreshing (with jQuery) the corresponsing div works too (debug-tested). What doesn't work - when refreshing (with jQuery) and printing the value of a PHP function, it doesn't refresh the function itself (its just printing the same thing over and over again). Note: I do not get any errors from the client and server consoles.
Here is the code:
PHP - getting the amount of players across servers:
function getPlayersTotal() {
$total = 0;
foreach (getServers() as $serverInfo) {
if (isOnline($serverInfo)) {
$ping = json_decode(file_get_contents('http://api.minetools.eu/ping/' . $serverInfo), true);
$total += intval($ping['players']['online']);
}
}
return $total;
}
JS - refreshing the - located in HEAD.
<script>
$(document).ready(
function() {
setInterval(function() {
var players = <?php print "" . getPlayersTotal() ?>;
$('#playersPLZ').text(
"" + players);
}, 3000);
});
</script>
Can anyone explain to me if (1.) is it possible to refresh the values - somehow - of the PHP function, and (2.) how to do so?
Regards.