Take the 2-minute tour ×

I have a problem that may not have a solution, but hopefully someone out there can figure this out.

I developed a website in PHP/MySQL that uses HTML/CSS to process payroll. When the user submits the payroll for the past (2 week) period, it processes each employee's hours. For companies with <50 employees, it can process it pretty fast, but for companies with over 100 employees, it can take quite a while to process. What I would like ideally is not a generic 'Loading' bar or an estimated '35% loaded' bar since each company's payroll will vary greatly in employee numbers.

The best solution would be that as soon as they submit the pay period, I could pass the total record number from the PHP/MySQL processor/DB, then update the number as each employee is processed from the PHP processor, so the user would see "Processing Employee 35 of 134" for example where '35' would increment and be updated as each record is processed. Or, if not possible, I'd even be fine with a dynamic list such as:

Processing Employee 1 of 134
Processing Employee 2 of 134
Processing Employee 3 of 134
Processing Employee 4 of 134
and so on ...

Ajax or Javascript seem to be the best options to achieve this, however I can't figure out yet how to use them to achieve this. Any help or insight would be greatly appreciated. I'll continue looking and update this post if I find anything as well.

share|improve this question

3 Answers 3

I've done that by calling the flush() command in PHP while iterating through the batch, but you could get tricky and update a hidden field and have a javascript function on setTimeOut check that value and update a progress bar.

And progress bar:

What I would do with the dynamic list is:

 $count = 0;

 // some db query

 // start output
 echo "<ul>";

 // iterate through records and perform dynamic insert

   $count ++;
   echo "<li>Processed " . $count . " records.</li>";

 // end iteration

 // end output
 echo "</ul>";

If you want to only update every % of records, then like you stated get a total count, then perhaps use a modulus operator in if clause. For example if you had 50 records and you wanted to update every 5, if($count mod 5 == 0) { echo ... flush() }

share|improve this answer
Thanks Mike, but it would seem that it's still processing it all first, and then displaying the above 'Processed count records' text after the page refreshes, as you can see by linkmy sample here. –  Evan Drury Jul 13 '12 at 18:57
@EvanDrury Does my attempted answer work for what you need? –  ImmortalFirefly Jul 13 '12 at 20:30
Evan, see the php flush link above and in the comments, you may need to enable ob_flush. It's environment-dependent and sometimes depending on your host, you may have to modify via .htaccess to enable as noted in the comments in that php article. It could be output_buffering or gzip, but there are tips to override. –  Mike S. Jul 14 '12 at 3:36

You would have to make a combination of what Mike S. suggested and quick ajax calls (say every 500 ms) You could make an ajax call to a text file that is written to from your PHP file....

For example:

    $count = 0;

 // start output

$query = mysql_query("SELECT ...");
while($rs = mysql_fetch_assoc($query)) {
    $fh = fopen('filename.txt','w');
    fwrite($fh, $count);

Then you need to make an ajax call every 500 ms (or sooner than that) to that filename.txt file and read the contents of that file to see how far along you are in processing your request. You could even do something similar to write in the contents of the php file [current_count]-[total_count] (15-155 for on record 15 of 155 total records) and do results.split('-') in your javascript coding.

share|improve this answer
@Evan Drury Let me know if you need the code fleshed out. Are you using a Javascript Library or plain javascript? –  ImmortalFirefly Jul 13 '12 at 20:29

My approach would be to store the total number of records and the current record number in session variables. Then set up a php page that returns the text/html of "Processing employee $currentRec of $totalRec".

When you submit the request from the main page, display a div on the page to show the status message. Fire off an ajax request to process the data and have it hide the div when it is complete. The code that processes the records can update the session variable as it goes along. At the same time fire off a periodical ajax request that gets the status message and updates the div's contents with the response. Have this continue until the div is no longer visible. You should have a status message on the page that pops up while the data is being processed to display the current record number, and it will update as often as you like based on how you set up the update timer.

The exact implementation would depend on whether you are using jQuery, prototype, plain Javascript, etc...

share|improve this answer

Your Answer


