I have a script, that might take 30 seconds to finish. Instead of let people wait for 30 seconds, I'd like to show a kind of progress status when the PHP script is running.

Say in my script:

for($i=0; $i<100; $i++)
echo "Running at # $i<br>";

What I want to achieve is instead of showing all these lines "Running at ..." all at once after the loop finishes, I want it to show one by one...

How do I do this? What HTML tags/code should I use to reflect the progress of a running PHP script?

Thanks.

    Hey,

    I believe you can get that information with the Zend Engine. Have a look at their site here. However if you wish to do it live (print out while the script is running it would have to be an nph.

      what is nph? looked all over Zend website, can't find how it can help...

        Originally posted by gekofish

        for($i=0; $i<100; $i++)
        echo "Running at # $i<br>";

        What I want to achieve is instead of showing all these lines "Running at ..." all at once after the loop finishes, I want it to show one by one...

        Thanks.

        Follow each such "progress notice" echo with a flush(); command. Then the server will try and push it as far out as it can to the browser immediately, instead of waiting in case there's further output.

          If you use the flush() directive to output status messages to the browser, be sure to enclose the status message inside a completed html container such as:

          print "<P>Please wait. Processing...</P>";

          If you don't, some browsers will not display it until the entire page loads (or some outer container is complete). Also, make sure that you are not outputting the messages inside some other container such as a table. If you do, nothing will display until you output the closing tag of the outside container.

          -- Rich

            Another cheats way of doing it is by creating a animated gif of a progress bar that fills up.

            Then just display that gif at the top of your HTML page, before you do any PHP processing. Then using Javascript and the body OnLoad event swap the image source to a spacer gif

            <html>
            <head>
            <title>progbar Test</head>
            </head>
            
            <body onload="document.ProgBar.src='/images/spacer.gif';">
            <IMG SRC="/images/rpt_prog_bar.gif" NAME="ProgBar" ALT="Generating Report, Please Wait...">
            
            <?
             //Do all of your PHP here
            ?>
            
            </body>
            </html>
            

            That way the gif just loops until the browser hits the end body html tag.

              7 years later

              Hey there. I have a 30 second script that is a killer to sit and wait on, so I added a cool loading indicator (attached, use if you want) and set up the following - which will work in all browsers, unlike the swapping out of the src from a previous commentor:

              <html>
              <head>
              <title>Switcheroo</head>
              </head>
              
              <body>
              <form method="post"><input type="submit" value="Find Results" onClick="document.getElementById('progBar').style.display='block'" /></form>
              
              <div style="display:none" id="progBar"><img src="counter1.gif" /></div>
              
              <?php
              /*start off your entire script with a wait on them to push the button.... */
              if($_SERVER['REQUEST_METHOD']=='POST') {
              
                /*do all of your script here, then put in the js to make the image disappear at the end of the long script.*/
              
                echo "<script language='javascript'>document.getElementById('progBar').style.display='none'</script>";
              }
              ?>
              
              </body>
              </html>
              

              Enjoy! Hope it saved you some time.

                Write a Reply...