如何在PHP中清除/删除/编辑echo

如何在PHP中清除/删除/编辑echo

问题描述:

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:

Page Of Site

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.

对于我的网站,我回应了$玩家。 问题是这不是替换回声而是创建一个新回声。 如何清除旧回声或删除旧回声或编辑它以使其只显示一个回声。 p>

如果你不明白我的意思(我不知道) 骂你),这是网站: strong> p>

网站页面 p>

代码: strong> p>

  while(isset($ _ GET ['hello'])){
 sleep(1); 
 $ result = $ conn-&gt; query(”SELECT id FROM gamecode WHERE gamecode ='$ gameCode  ““);  
 $ players = $ result-&gt; num_rows;  
 echo“&lt; h4&gt; $ players Players&lt; / h4&gt;”; 
 flush(); 
} 
  code>  pre> 
 
 

非常感谢你 如果有任何帮助,请给我。 strong> p> div>

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

remove the echo statement within your while loop because the code will always be triggered so long as your hello is true