[英]How to clear/remove/edit echo in PHP
For my website I echo out $players. 对于我的网站,我回显了$ 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. 您将需要服务器端(PHP)和客户端(javascript)来实现此目标。
Server side: only echo once, discard the while loop; 服务器端:仅回显一次,丢弃while循环;
Client side: call server side (ajax or refresh the whole page) every n second to get the latest data. 客户端:每隔n秒钟调用服务器端(ajax或刷新整个页面)以获取最新数据。
Example: 例:
(1) Create a new php file "nowStat.php", and move your db query into this file, like: (1)创建一个新的php文件“ nowStat.php”,并将您的数据库查询移到该文件中,例如:
$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: (2)在您的createcode.php文件中,使用ID定义一个空的
<h4>
或<div>
:
<h4 id="playerStatus"></h4>
Then in the same file use jQuery/Ajax to retrieve the status every 5 sec: 然后在同一文件中,每5秒使用jQuery / Ajax检索状态:
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 这是关于jQuery / Ajax的非常简单的教程: https : //www.w3schools.com/jquery/jquery_ajax_intro.asp
在while
循环中删除echo
语句,因为只要hello
,代码就会始终被触发
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.