1

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.

Momo
  • 3,542
  • 4
  • 21
  • 34
  • You're messing client side with server side code (JS with PHP). What you should aim is to make an AJAX call to the server every 3 secons, what you're doing is calling the same value every 3 secons (see your generated HTML file in your browser...). [Here](http://stackoverflow.com/questions/7165395/call-php-function-from-javascript), you can find more info. – mTorres Aug 02 '14 at 17:39

1 Answers1

0

This is a job for Ajax. Here's what's happening now: your PHP code is outputting the number of players directly into the JavaScript that's sent to the browser. If the number of players is 50, then all the browser sees is this:

<script>
    $(document).ready(
        function() {
            setInterval(function() {
                var players = 50;
                $('#playersPLZ').text(
                        "" + players);
            }, 3000);
        });
</script>

So the same function, with the number 50, is run every three seconds. The browser doesn't know anything about your PHP, it just sees the number 50. For all the browser knows, it's the same as if the number 50 were hardcoded in.

What you need to do is to send a request back to the server every 3 seconds asking for the new number, since your server is where the PHP code is run.

You need to create a new PHP file. I'll call it num-players.php. That file should output the number of players. You could do something along these lines:

<?php
    // You will need to require the file containing your getPlayersTotal() function
    echo getPlayersTotal();
?>

Then, your JavaScript can be something like this:

<script>
    $(document).ready(function() {
        setInterval(function() {
            $.get("num-players.php", function(players) {
                $("#playersPLZ").text(players);
            });
        }, 3000);
    });
</script>

This will send a request back to your server every 3 seconds, asking for the new number. The code in num-players.php will return that number, and when the request completes, your element will be updated with the number.

Josiah Keller
  • 3,635
  • 3
  • 23
  • 35