2

I am trying to run example from http://w3shaman.com/article/php-progress-bar-script.

I am using quickPHP on windows. Above tutorial has live Demolink which is running fine.

Problem:

When I test this script, I get bar only when PHP is done processing. I also tried using jQuery UI progress-bar but PHP gets busy before displaying progress bar and then suddenly shows 100 completion at end. What is wrong ?

Code from above link:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    <title>Progress Bar</title>
</head>
<body>
<!-- Progress bar holder -->
<div id="progress" style="width:500px;border:1px solid #ccc;"></div>
<!-- Progress information -->
<div id="information" style="width"></div>
<?php
// Total processes
$total = 10;

// Loop through process
for($i=1; $i<=$total; $i++){
    // Calculate the percentation
    $percent = intval($i/$total * 100)."%";

    // Javascript for updating the progress bar and information
    echo '<script language="javascript">
    document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.';background-color:#ddd;\">&nbsp;</div>";
    document.getElementById("information").innerHTML="'.$i.' row(s) processed.";
    </script>';

    // This is for the buffer achieve the minimum size in order to flush data
    echo str_repeat(' ',1024*64);

    // Send output to browser immediately
    flush();

    // Sleep one second so we can see the delay
    sleep(1);
}

// Tell user that the process is completed
echo '<script language="javascript">document.getElementById("information").innerHTML="Process completed"</script>';
?>
</body>
</html>

3 Answers 3

1

I think right now, QUick PHP doesn't let you get intermediate results as stated here: http://www.zachsaw.com/forum/viewtopic.php?f=11&t=27&hilit=flush (I know it's an old post, but I couldn't find any recent reference to flush use on QuickPHP).

It seems that because of the way QuickPHP handles the output of PHP, it only sends a result when the complete script is done executing. That's why you're getting the bar complete, because your script relies on the possibility to keep sending data to the web browser when the php script is still running.

So the answer is that, it is not possible right now to do it that way.

Sign up to request clarification or add additional context in comments.

Comments

0

Try to use this one:

//At the beginning of your PHP file
ob_start();
...

And then:

ob_flush();
flush();

instead of

flush();

2 Comments

It's quite weird: it works fine to me. Although I have an idea: try don't using echo, something like this: ... ?> <script>your_js_code_here</script> <? ...
Nope, I use a full-fledged Apache server for my projects
0

I had a similar situation at my work. I used jquery, json to check progress of scripts execution. The php script writes a temporary file its status. I blogged about it, there may be some bug here there but does the job quite well.

Comments

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.