PHP实时输出内容

发布于 2024-10-05 09:17:03 字数 463 浏览 0 评论 0原文

我正在尝试使用循环向访问者实时显示一些内容。问题在于内容被添加到输出中而不是被新内容替换。下面是从 10 计数到 0 的代码示例。输出显示整个结果集,而不是向后计数。我的意思是,我不想看到计数器显示的所有数字只是每个数字。

ob_end_flush ();
        //start buffering
        ob_start ();

        echo str_pad ( '', 1024 ); // minimum start for Safari
        for ( $i = 10; $i > 0; $i --) {
            echo str_pad ( "$i<br>\n", 8 );
            ob_flush ();
            flush ();
            sleep ( 1 );
        }
        die ();

I'm trying to display some content in real time to my visitors using a loop. Problem is that the content is added to the output instead of being replaced with the new one. Here's an example of code which counts from 10 to 0. The output shows the whole set of results instead of counting backwards. I mean, I don't want to see all numbers shown just each number as the counter goes.

ob_end_flush ();
        //start buffering
        ob_start ();

        echo str_pad ( '', 1024 ); // minimum start for Safari
        for ( $i = 10; $i > 0; $i --) {
            echo str_pad ( "$i<br>\n", 8 );
            ob_flush ();
            flush ();
            sleep ( 1 );
        }
        die ();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不即不离 2024-10-12 09:17:03

仅使用 PHP 无法实现这一点,因为更改浏览器中呈现的内容需要一些客户端干预。

如何进行这种分工取决于需要什么。

如果你的代码就是这一切,那么你最好编写一个 JS 例程来以定时方式对数字进行计数。

如果需要 PHP 以其他方式生成数字,则需要决定是否可以将更新的生成拆分为多个 PHP 调用,或者由于操作而必须来自单个 PHP 调用在那次召唤中发生。

如果是前者,您可以将其分开,这是理想的情况 - 那么过程是设置 AJAX 调用以定期检索更新的信息。

如果后者必须来自单个页面,则必须保持连接打开并生成新内容。

在这种情况下,要获取新内容来替换旧内容,JS 代码必须处理此问题。定期在页面上运行的代码可以检查新内容并相应更新,或者可以玩一些小技巧注入执行更新的代码。

发送执行更新的内联

This can not be achieved with PHP alone, as to alter what is rendered in the browser requires some client side intervention.

How you go about this division of labour depends upon what is needed.

If your code is all of it, then you would be far better off writing a JS routine to count down the numbers in a timed fashion.

If PHP is required in some other fashion to generate the numbers then the decision needs to be taken as to whether you can split the generation of the update in to multiple PHP calls or if it has to be from within the single PHP call due to actions taking forth within that call.

If the former, you can separate it, which is the ideal situation - then the procedure is to setup AJAX calls to retrieve updated information periodically.

If the latter and it must be from a single page, then the connection must be kept open with new content generated.

In this case, to get new content to replace old, JS code must handle this. Either code running periodically upon the page can check for new content and update accordingly, or a little trick can be played injecting code that performs the update.

Sending inline <script> content that performs the update instead of new display elements works well, if a little untidy, as the browser receives the new <script> element it is run. This is the only way I am aware of that a push-update can be made to a web page without plugins.

私野 2024-10-12 09:17:03

可能有一些技巧或其他东西(只是想到一两个),但最合适的方法是使用浏览器的 ajax 自动更新。就像这个模式描述一样。

There might be a trick or something (just thought of one or two) but the most appropriate way to do it is with an ajax autoupdate from the browser. Like this pattern description.

帅冕 2024-10-12 09:17:03

我们没有在 PHP 中这样做,实际上是使用 js。 (在 Stacker 的帮助下)您可以反转计数

但我确信您可以采用,如果这不能回答您的问题,抱歉。

我们的开发网站上有演示 < 链接 >

您也可以(同样可能不相关,使用 jquery 刷新 div 元素)

We didnt do this in PHP, in fact using js. ( with Stacker help ) you can reverse the count

But am sure you could adopt, sorry if this doesnt answer your question.

Demo here on our dev site < link >

You can also ( again may not be pertinent, use jquery to refresh a div element )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文