Reputation: 11
For my website I echo out $players. The problem is that this instead of replacing the echo it creates a new one. How can i clear the old echo or delete the old one or edit it so it only has one echo shown.
If you don't understand what I mean (I don't blame you), this is the website:
Code:
while(isset($_GET['hello'])) {
sleep(1);
$result = $conn->query("SELECT id FROM gamecode WHERE gamecode = '$gameCode'");
$players = $result->num_rows;
echo "<h4>$players Players</h4>";
flush();
}
Thank you so much for any help you give me.
Upvotes: 0
Views: 1501
Reputation: 136
You will need both server side (PHP) and client side (javascript) to achieve this goal.
Server side: only echo once, discard the while loop;
Client side: call server side (ajax or refresh the whole page) every n second to get the latest data.
Example:
(1) Create a new php file "nowStat.php", and move your db query into this file, like:
$result = $conn->query("SELECT id FROM gamecode WHERE gamecode = '$gameCode'");
$players = $result->num_rows;
echo "$players Players";
(2) In your createcode.php file, define an empty <h4>
or <div>
with an id:
<h4 id="playerStatus"></h4>
Then in the same file use jQuery/Ajax to retrieve the status every 5 sec:
setInterval(function() { $("#playerStatus").load("nowStat.php"); }, 5000);
Here is a very simple tutorial about jQuery/Ajax: https://www.w3schools.com/jquery/jquery_ajax_intro.asp
Upvotes: 1
Reputation: 326
remove the echo
statement within your while
loop because the code will always be triggered so long as your hello
is true
Upvotes: 0