2
for($x=0;$x<100000;$x++) {
    echo "hello $x";
}

So, normally, it will finish processing everything first and then print everything at once. Is there a way to just keep printing the message as the command is sent, so that I'll see a new hello $x appended one by one or do I have to use jQuery/JavaScript?

Update:

I ask because I'm trying to run a long test script and have it show all the results 1 by 1 on the browser so I don't have to go check the logs and could just visually see the color coded results. I'm trying to make a similar effect to this: http://tools.css3.info/selectors-test/test.html If anyone could provide a short sample of the jQuery (if it has to be done this way), I would appreciate it.

3
  • PHP processes it and sends the output to your web browser. Your web browser might render it instantly, or it might render it in huge chunks. I suggest you use jQuery to perform this. Commented May 13, 2012 at 3:35
  • Because http is request-response system, I believe that it just possible by using ajax. Commented May 13, 2012 at 3:40
  • It will work in the command line like that im pretty sure? Commented May 13, 2012 at 3:54

2 Answers 2

3

Although it's possible by controlling the output buffer, I wouldn't do that, mainly because it will delay the JavaScript DOMReady event.

If you're looking for a visual effect, you should use JavaScript (but I don't see any reason for Ajax based on what your question says). It can be accomplished with a simple setInterval. Considering an initially empty <div id="hello">:

var i = 0;
var timer = setInterval(function(){
    if(i == 10000) {
        clearInterval(timer);
        return;
    }
    var div = document.getElementById('hello');
    div.innerHTML += 'hello ' + i + '<br>';
    i++;
}, 250);

I just saw your edit, and now I think you actually should use Ajax! The example you linked to does. Basically, you have to setup a test queue in JavaScript, where each test has an unique URL. Then it's just a matter of firing one request at a time with Ajax (jQuery and $.get would be the easiest way to go). The following assumes an <ul> instead of the div from my previous example, and that the server will respond with a success or failure message for each test:

var tests = [
    'http://example.com/test1',
    'http://example.com/test2',
    'http://example.com/test3',
];

function processQueue() {
    if(tests.length > 0) {
        var test = tests.shift();
        $.get(test, function(response) {
            $('#some_ul').append('<li>' + response + '</li>');
            processQueue();
        });    
    }
}

processQueue();
Sign up to request clarification or add additional context in comments.

1 Comment

I changed my mind after your edit, this is not a mere visual effect. The thing is, you don't just show the results one by one, you actually process them one by one, asynchronously.
1

Yes, if you disable output buffering: http://php.net/manual/en/book.outcontrol.php

But as @Blender suggests you'd probably be better off doing this with AJAX (commonly done using jQuery).

1 Comment

@Strawberry what exactly are you trying to do? There's lots of tutorials out there for doing ajax with PHP + jQuery, have a look around, if they don't answer your question please expand your question above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.