"Loading record number [##] from [Total]" of the progress bar

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

I developed a website in PHP / MySQL that uses HTML / CSS for payroll processing. When a user sends a payroll for the past (2 weeks) period, he processes every working hour. For companies with 50 employees, it can process it fairly quickly, but for companies with more than 100 employees, it can take quite a lot of time to process. Ideally, I would prefer a non-general β€œDownload” template or an estimated β€œ35% loaded” panel, since each payroll of each company varies greatly in the number of employees.

The best solution would be that as soon as they send the payment period, I can transfer the full record number from the PHP / MySQL / DB processor, and then update the number, since each employee is processed from the PHP processor, so the user will see "Employee Processing 35 of 134 ", for example, where" 35 "will increase and update as each record is processed. Or, if this is not possible, I will even be fine with a dynamic list, for example:

Processing employee 1 of 134
Processing employee 2 of 134
Employee Handling 3 of 134
Employee Handling 4 of 134
etc...

Ajax or Javascript seems to be the best options for achieving this, however I still cannot figure out how to use them to achieve this. Any help or understanding will be appreciated. I will continue to search and update this post if I find anything else.

+4
source share
3 answers

I did this by calling the flush () command in PHP during iteration through the batch, but you can get a difficult task and update the hidden field and have the javascript function in setTimeOut, check this value and update the progress bar.

And a progress bar:

What I would do with a dynamic list:

$count = 0; // some db query // start output echo "<ul>"; // iterate through records and perform dynamic insert $count ++; echo "<li>Processed " . $count . " records.</li>"; flush(); // end iteration // end output echo "</ul>"; 

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

0
source

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

For instance:

 <?php $count = 0; mysql_connect('blahblah'); // start output $query = mysql_query("SELECT ..."); while($rs = mysql_fetch_assoc($query)) { $fh = fopen('filename.txt','w'); fwrite($fh, $count); fclose($fh); ++$count } ?> 

Then you need to make an ajax call every 500 ms (or earlier) to the file filename.txt and read the contents of this file to find out how far your request is going. You can even do something similar to writing in the contents of the php file [current_count] - [total_count] (15-155 to record 15 out of 155 shared records) and make results.split('-') in your javascript encoding.

0
source

My approach was to keep the total number of records and the current record number in session variables. Then set up a php page that returns the text / html "Employee Handling $ currentRec from $ totalRec".

When you submit a request from the main page, display a div on the page to display a status message. Turn off the ajax request for data processing and hide it when it is complete. The code that processes the records can update the session variable as it moves. At the same time, run a periodic ajax request that receives a status message and updates the contents of the div with the response. Do this until the div is no longer visible. You should see a status message on the page that appears during data processing to display the current record number, and will be updated as often as you like, based on the update timer setting.

The exact implementation will depend on whether you use jQuery, prototype, simple Javascript, etc.

0
source

All Articles