5

How can I update a progress bar (html), during a long loop (php -- database export/import mysqli)

I don't want to use a file or database for storing temporary values or progression of the loop as that causes too many IO's to be used (and in the case of file based solution, the client ends up often reading the file before the data has been written)

I was considering some event type situation however output buffering takes too long to update the display.

Second, I was considering some methods others have imposed of spitting out a javascript to the browser, but that ends up in way too much HTML being output (aka, 1,000,000+ records being dumped).

Basically, I am open to fresh ideas on how I can update the progress bar in a web-browser while some really long process is happening in PHP (this is not file_upload stuff -- I realize there are extensions for PHP which allow for getting the progress, however there doesn't seem to be anything for getting the progress of a server loop).

Any ideas ?? --- i really don't want to stray from PHP/JavaScript/HTML to other platforms such as node.js / cocoa / etc.

Kraang Prime
  • 9,981
  • 10
  • 58
  • 124
  • Given the requirements that you've outlined above, I can only recommend WebSockets to achieve what you're trying to do. That being said, implementing WebSockets may be impractical for achieving just this one particular solution. – Ohgodwhy Apr 12 '14 at 21:17
  • Neither using `[...]file or database for storing temporary values[...]` nor using e.g. chunked encoding would require you to make a update for every step you do on the server while you process the data. You will only need to update the values when you want to notify the user. E.g. if you process `1,000,000` records you would update the status for ever `1,000` or `10,000` record. – t.niese Apr 12 '14 at 21:21
  • @t.niese - I tried that before by doing `$x % 10000` before file-write, but still ended up with failed reads (empty file) on client side due to the brief moment in between the file open, clear, write, and save. – Kraang Prime Apr 12 '14 at 21:48

1 Answers1

2

You could flush your content as your script´s execution progress.

Here are some useful links to good code samples that may solve your problem:

You could obtaing other ideas in this topic here: Show progress for long running PHP script

Community
  • 1
  • 1
  • 1
    Looks VERY promising -- and that was just the first link. Give me a bit to test these out. This definitely looks like a winning answer tho. – Kraang Prime Apr 12 '14 at 21:51
  • 2
    #1 works, but --- it does so outputting JavaScript to the page. Seems like the best solution so far tho. – Kraang Prime Apr 12 '14 at 21:55
  • 1
    You should no only link again external sites, as they could go away, but summarize the techniques, so that a reader will get at least a rough idea about them. SO should be an independent knowledgebase. – t.niese Apr 13 '14 at 08:15
  • Good advice @t.niese , i will try to improve on that in my next answers. Thanks. – bovino Marcelo Bezerra Apr 13 '14 at 19:42