通过php类功能循环打印进度 [英] print progress as looping through php class functions

查看:96
本文介绍了通过php类功能循环打印进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我正在使用aOuth和其他服务器进行某些操作,尽管我创建的类可以正常工作,但它并没有使HTML页面保持进度.例如说我有以下课程:

I am doing something with aOuth and some else's server at the moment and while the class I've created works it doesn't keep the HTML page posted of it's progress. For example say I have the following class:

class Something {
    function a() {
        sleep(2); echo "a()"; return TRUE;
    }
    function b() {
        sleep(2); echo "b()"; return TRUE;
    }
    function c() {
        sleep(2); echo "c()"; return TRUE;
    }
}

然后我遍历HTML中的类:

Then I loop through the class in my HTML:

$something = new Something();
if($something->a()) {
    if($something->b()) {
        if($something->c()) {
            echo "everything completed!!";
        }
    }
}

页面将呈现:

a()b()c()everything completed!!

6秒后.我希望它随其进行更新(即,当它完成处理某项-> a()时打印a(),当它完成处理某项-> b()等时打印b())

6 seconds later. I want it to update as it goes (i.e. print a() when it's finished processing something->a(), print b() when it's finished processing something->b(), etc...)

推荐答案

在Chrome 21,Firefox 15和IE8中为我工作:

Worked for me in Chrome 21, Firefox 15 and IE8:

<?php
header( 'Content-Type: text/html; charset=utf-8' );

class Something {
    function a() {
        echo "a()" . str_repeat(' ', 1024); 
        ob_flush(); flush(); 
        sleep(2); return TRUE;
    }
    function b() {
        echo "b()" . str_repeat(' ', 1024); 
        ob_flush(); flush(); 
        sleep(2); return TRUE;
    }
    function c() {
        echo "c()" . str_repeat(' ', 1024); 
        ob_flush(); flush(); 
        sleep(2); return TRUE;
    }
}

$something = new Something();
if($something->a()) {
    if($something->b()) {
        if($something->c()) {
            echo "everything completed!!";
        }
    }
}

说明:我们必须说服立即开始输出...

Explanation: we have to persuade into starting an output immediately...

Apache(网络服务器),并在脚本开始处发送Content-Type标头.

Apache (web-server), with sending Content-Type header right at the beginning of the script.

网络浏览器,因为其中一些浏览器除非足够大,否则不会考虑绘制部分输出.解决方法是将一个较大但空的字符串(str_repeat(' ', 1024))附加到输出中.

web-browsers, as some of them won't consider drawing a partial output unless it's big enough. The workaround is to append a sizable, but empty string (str_repeat(' ', 1024)) to the output.

这篇关于通过php类功能循环打印进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆