I believe what I need is to use the function flush(), but I don't understand how it should be done. Here is what I want to achieve:

As part of my code, I have a loop doing various stuff. For every iteration I increment a counter and I want to echo the value of this counter for every iteration. I believe I should use flush(), but how? Any examples would be great.

Thanks

    just increment it, then echo it out... um, not hard eh? But if you want to use flush(), you'd need to know how to use buffers.

    ob_start();
    for($i=0;$i<100;$i++) {
    $counter++;
    echo $counter;
    for($a=0;$a<50;$a++) {
    $counter2++;
    echo $counter2;
    }
    ob_end_flush();
    

    That would start a buffer, put it all in the buffer, and when ob_end_flush() is called, would print it all on the screen...

      here is another example of flush with each result from a query in a new table

      <?
      echo str_repeat(" ", 500);
      $conn = mysql_connect("localhost", "username", "password") or die ("Could not connect to database!");
      $db = mysql_select_db("databasename") or die ("Could not select database");
      
      $query = "select * from tablename;";
      $result = mysql_query($query);
      
      echo "Gathering Results...<br><br>";
      ?>
      
      <?
      while ($row = mysql_fetch_array($result))
      {
      sleep(1);
      ?>
      <table cellpadding="0" cellspacing="0" border="0">
      <tr><td><? echo $row['name']; ?></td></tr>
      </table>
      <?
      flush();
      }
      ?>

        not hard at all...

        for($i = 1; $i < $somevalue; $i++) {
        doSomething();
        echo $i . "<br>\n";
        flush();
        }

        every time one iteration finishes, it will print out the iteration number ($i) out on the browser.

          5 months later

          How can i display a message on the screen, saying "retrieving results from database" while my code loop though the query results, are not ready for showing in the screen, and when it is ready is presented on the screen?

            yes but what condition, must be checked to stay echoing a "wait a moment" message?

              this line here

              echo "Gathering Results...<br><br>";

              you can change to say whatever you want, other than that, it will display one row after another 1 second at a time, or to whatever second amount you put in the sleep() function

                sleep(1);

                How would you pause for half a second? .5 and 1/2 didn't work 🙂

                It is probably something simple but I have never used sleep before. Kinda nifty though.

                  Originally posted by eternalprophet
                  sleep(1);


                  How would you pause for half a second? .5 and 1/2 didn't work 🙂

                  It is probably something simple but I have never used sleep before. Kinda nifty though.

                  Simply put, you don't!!!

                  sleep
                  (PHP 3, PHP 4 )

                  sleep -- Delay execution
                  Description
                  void sleep ( int seconds)

                  The sleep() function delays program execution for the given number of seconds.

                    usleep() will do microseconds while sleep() will only do whole seconds

                      Going all the way back to your original question ...

                      To use flush(), just call the function.

                      BUT ...

                      It may not do what you want, which is to persuade both your Web server and the client's Web browser to immediately display data.

                      Read the ANNOTATED manual. It explains this in detail, and there are some interesting and helpful comments from users below the official description.

                        a month later

                        In my code i do one self-submitting (using javascript) to return some conditional data ( the streets from a choosed city ) before the user click the send button and send the complete filled form....

                        i would like to say " wait a moment retrieving data ... " how can i stop the wait message and go back to my form showing the streets given from a choosed city?

                        just putting my code inside

                        ob_start();
                        //code here
                        ob_end_flush();

                        ??????

                          Write a Reply...