如何使用php中的print语句和图像功能每隔几秒刷新一次div

时间:2022-10-28 01:24:35

I'm trying to build a browser card game with mostly PHP and maybe some other useful languages thrown in to test and expand my PHP knowledge with something I always wanted to make. But every online solution I've read and tried to implement wouldn't work and I don't know why. I have logic like this:

我正在尝试构建一个主要是PHP的浏览器卡片游戏,也许还有一些其他有用的语言可以用我一直想做的东西来测试和扩展我的PHP知识。但是我读过并试图实现的每个在线解决方案都行不通,我不知道为什么。我有这样的逻辑:

//find game form
        print "<form method='post' action='game.php'>
        <label>Enter host generated game #</label><br>
        <label><input type='text' name='gamenumber'></label><br>
        <label><input type='submit' name='findgame' value='FindGame'></label><br><br>
        </form>";

        //host game button
        print "<form method='post' action='game.php'>
        <label><input type='submit' name='hostgame' value='HostGame'></label>
        </form>";

//game logic



if(isset($_POST['hostgame'])) 
    {
        $roomnumber = rand();
        print $roomnumber;

        insertField($Ausername,$roomnumber); //insert field GUI

        //link game to host for future field printing
        $query = "UPDATE game".$roomnumber." SET host='".$Ausername."' WHERE host='null'";
        $result = mysqli_query($link, $query);

        print "<div id='quote'>"; //populate host just disappears <meta http-equiv='refresh' content='5' />

        //populates self and opponents in hosts perspsective
        populatehost($roomnumber); 

        print "</div>";


    }

    if(isset($_POST['findgame'])) //this processes after user submits data.
    {
        $roomnumber = $_POST['gamenumber'];
        $_SESSION['gamenumber'] = $_POST['gamenumber'];

        //link game to find for future field printing
        $query = "UPDATE game".$roomnumber." SET find='".$Ausername."'WHERE find='null'";
        $result = mysqli_query($link, $query);

        print "<form method='post' action='game.php'>
        <label><input type='submit' name='startgame' value='StartGame'></label>
        </form>";

        //populates self and opponents in finds perspsective
        populatefind($roomnumber); 

    }


    if(isset($_POST['startgame'])) //this processes after user submits data.
    {
        print "<br>Game started.<br>";

        print "heads or tails?\n\n";

        print "<form method='post' action='game.php'>
        <label><input type='text' name='headstails'></label><br>
        <label><input type='submit' name='select' value='Select'></label><br><br>
        </form>";

        //finder always starts game
        populatefind($_SESSION['gamenumber']);
    }

    $gameStatus="";
    $flip = rand(1,2);
    $coin = "heads";
    $gameStarted = "game not started";

    if($flip==1)
        $coin="heads";
    else if($flip==2)
        $coin="tails";

    if(isset($_POST['headstails'])) //this processes after user submits data.
    {
        if($_POST['headstails']==$coin)
        {
            print "find game player goes first";
            $gameStarted = "game started";
            //notify find game player he goes first
            //do logic where he goes first
        }
        else
        {
            print "host game player goes first";
            $gameStarted = "game started";
            //notify host game player he goes first
            //do logic where he goes first
        }

        populatefind($_SESSION['gamenumber']);
    }

this is the jquery function I tried that I pasted at the bottom of the page.

这是我尝试过的粘贴在页面底部的jquery函数。

?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js%22%3E%3C/script%3E">
    jQuery(function () {
        var $els = $('div[id^=quote]'),
            i = 0,
            len = $els.length;

        $els.slice(1).hide();
        setInterval(function () {
            $els.eq(i).fadeOut(function () {
                i = (i + 1) % len
                $els.eq(i).fadeIn();
            })
        }, 2500)
    })
</script>

</body>
</html>

so I was able to get the field printed for both the host and finder, and once the finder flips the coin it says who goes first from their perspective.. but I'm having trouble getting that to appear on the other player's perspective. I was thinking of implementing that by refreshing a div with the function that does all the printing, but nothing seemed to work no matter what. I tried the php sleep method in many different ways, I tried some jquery function, nothing worked. And btw populate host and populate find are just functions with giant tables based on arrays of variables.

因此,我能够为主持人和发现者打印字段,一旦取景器翻转硬币,就会说出谁从他们的角度出发,但是我很难将其显示在另一个玩家的角度。我正在考虑通过使用执行所有打印的功能刷新div来实现它,但无论如何似乎没有任何工作。我尝试了很多不同的PHP睡眠方法,我尝试了一些jquery函数,没有任何效果。而btw填充主机和填充查找只是基于变量数组的巨型表的函数。

2 个解决方案

#1


7  

This sort of thing you're trying to achieve will spend a lot of resources of your server using regular Ajax calls.

您尝试实现的这类事情将使用常规Ajax调用来消耗服务器的大量资源。

Similar Question : Link

类似问题:链接

Let's consider a chat between two users as example:

我们以两个用户之间的聊天为例:

User's 1 page have a Javascript timer asking every second for new messages: "hey server, send me new messages." With or without new messages, the server will handle the request, search for new messages to reply "I've got no messages for you right now" or "here is your new message". Same for user 2.

用户的1页有一个Javascript计时器,询问每一秒的新消息:“嘿服务器,给我发新消息。”无论是否有新消息,服务器都会处理请求,搜索新消息以回复“我现在没有消息”或“这是您的新消息”。对于用户2也是如此。

If both users keep the chat page open for 10 seconds, your server will receive 20 requests for new messages, will run 20 searches for new messages and will send 20 replies with the new messages or "no new messages" reply. Even if both users had send nothing, just keeping the page open. Imagine if 1000 users are connected to your chat room!

如果两个用户都将聊天页面保持打开10秒钟,则您的服务器将收到20条新消息请求,将搜索20条新消息,并将发送20条带有新消息的回复或“无新消息”回复。即使两个用户都没有发送任何内容,只需保持页面打开即可。想象一下,如果有1000个用户连接到您的聊天室!

The best approach in this case is Socket. In the same chat example, user 1 and user 2 will stay listening for new messages instead asking for them each second. When user 1 send a message, the server will notify the user 2 "I have a new message for you. Here is."

在这种情况下,最好的方法是Socket。在同一个聊天示例中,用户1和用户2将继续收听新消息,而不是每秒都要求他们。当用户1发送消息时,服务器将通知用户2“我有一条新消息。这是。”

If 1000 users are connected, there is no problem at all. The server will send the new message to a socket channel and all users connected will be notified. No delay.

如果连接了1000个用户,则完全没有问题。服务器将新消息发送到套接字通道,并且将通知所有连接的用户。没有延迟。

In your specific case, will work like this: player 1 and player 2 are listening for new messages. When player 1 clicks on card, will trigger a command for server. Server will receive and send a message for a socket channel: "hey everyone who is awaiting for new cards, here is" then every player will be notified. Instantly.

在您的具体情况下,将这样工作:播放器1和播放器2正在侦听新消息。当玩家1点击卡片时,将触发服务器命令。服务器将接收并发送套接字通道的消息:“嘿,等待新卡的所有人,这里是”,然后每个玩家都会收到通知。即刻。

Another problem with the first scenario, using Javascript timers: if you increase the timer interval, the replies will be delivery delayed.

使用Javascript计时器的第一个场景的另一个问题是:如果增加计时器间隔,则回复将延迟传递。

Another good thing about sockets: it's independent of technology. PHP, Java, Node, etc.

套接字的另一个好处是:它独立于技术。 PHP,Java,Node等

GitHub Project for using WebSocket with Php : Link

使用WebSocket和Php:Link的GitHub项目

You can see some socket demos here

你可以在这里看到一些套接字演示

Here is a repository with some tests I ran when I tried Socket earlier

这是一个存储库,其中包含我在之前尝试过Socket时运行的一些测试

#2


3  

You can do it this way:

你可以这样做:

setInterval(function refresh(){
  var value = Math.round(Math.random()*100);
  $("#target").addClass("hidden");
  $("#target").html(value);
  $("#target").removeClass("hidden");
}
, 1500);
.hidden {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="target">
</div>

#1


7  

This sort of thing you're trying to achieve will spend a lot of resources of your server using regular Ajax calls.

您尝试实现的这类事情将使用常规Ajax调用来消耗服务器的大量资源。

Similar Question : Link

类似问题:链接

Let's consider a chat between two users as example:

我们以两个用户之间的聊天为例:

User's 1 page have a Javascript timer asking every second for new messages: "hey server, send me new messages." With or without new messages, the server will handle the request, search for new messages to reply "I've got no messages for you right now" or "here is your new message". Same for user 2.

用户的1页有一个Javascript计时器,询问每一秒的新消息:“嘿服务器,给我发新消息。”无论是否有新消息,服务器都会处理请求,搜索新消息以回复“我现在没有消息”或“这是您的新消息”。对于用户2也是如此。

If both users keep the chat page open for 10 seconds, your server will receive 20 requests for new messages, will run 20 searches for new messages and will send 20 replies with the new messages or "no new messages" reply. Even if both users had send nothing, just keeping the page open. Imagine if 1000 users are connected to your chat room!

如果两个用户都将聊天页面保持打开10秒钟,则您的服务器将收到20条新消息请求,将搜索20条新消息,并将发送20条带有新消息的回复或“无新消息”回复。即使两个用户都没有发送任何内容,只需保持页面打开即可。想象一下,如果有1000个用户连接到您的聊天室!

The best approach in this case is Socket. In the same chat example, user 1 and user 2 will stay listening for new messages instead asking for them each second. When user 1 send a message, the server will notify the user 2 "I have a new message for you. Here is."

在这种情况下,最好的方法是Socket。在同一个聊天示例中,用户1和用户2将继续收听新消息,而不是每秒都要求他们。当用户1发送消息时,服务器将通知用户2“我有一条新消息。这是。”

If 1000 users are connected, there is no problem at all. The server will send the new message to a socket channel and all users connected will be notified. No delay.

如果连接了1000个用户,则完全没有问题。服务器将新消息发送到套接字通道,并且将通知所有连接的用户。没有延迟。

In your specific case, will work like this: player 1 and player 2 are listening for new messages. When player 1 clicks on card, will trigger a command for server. Server will receive and send a message for a socket channel: "hey everyone who is awaiting for new cards, here is" then every player will be notified. Instantly.

在您的具体情况下,将这样工作:播放器1和播放器2正在侦听新消息。当玩家1点击卡片时,将触发服务器命令。服务器将接收并发送套接字通道的消息:“嘿,等待新卡的所有人,这里是”,然后每个玩家都会收到通知。即刻。

Another problem with the first scenario, using Javascript timers: if you increase the timer interval, the replies will be delivery delayed.

使用Javascript计时器的第一个场景的另一个问题是:如果增加计时器间隔,则回复将延迟传递。

Another good thing about sockets: it's independent of technology. PHP, Java, Node, etc.

套接字的另一个好处是:它独立于技术。 PHP,Java,Node等

GitHub Project for using WebSocket with Php : Link

使用WebSocket和Php:Link的GitHub项目

You can see some socket demos here

你可以在这里看到一些套接字演示

Here is a repository with some tests I ran when I tried Socket earlier

这是一个存储库,其中包含我在之前尝试过Socket时运行的一些测试

#2


3  

You can do it this way:

你可以这样做:

setInterval(function refresh(){
  var value = Math.round(Math.random()*100);
  $("#target").addClass("hidden");
  $("#target").html(value);
  $("#target").removeClass("hidden");
}
, 1500);
.hidden {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="target">
</div>