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.
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
在while
循环中删除echo
语句,因为只要hello
,代码就会始终被触发
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.