0

I have a PHP script that takes a long time to execute because every operation has to do SOAP call to another website. I would like to add a progressbar indicator so that I can see the % complete of this script.

I believe the way to do this is to do do an asynchronous Ajax call that executes the PHP and then have another Ajax call that updates the % complete. But I am having a lot of problems putting this together.

So far I have only this:

<script language='Javascript'>

$().ready(init);

function init() {       
    $.get('importStatement.php');           
}

$(function() {
$( "#progressbar" ).progressbar({
  value: 0
});
});

</script>

<div id="progressbar"></div>

My PHP script that outputs the percentage complete ($i):

$current_day = $date->today;
$days_to_read = 45;
// Scroll through the last $days_to_read dates and import a statement for that day
for ($i = 0; $i < $days_to_read; $i++) {
    $statement_date = $date->convertToMysql($current_day);
    $bank->importStatement($statement_date, true);
    $percent = intval($i/($days_to_read-1) *100).  "%";
    // disable caching
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
    header('Cache-Control: no-cache, must-revalidate');
    header('Pragma: no-cache');
    header('Expires: Mon, 26 Jul 1991 05:00:00 GMT');  // disable IE caching
    header('Content-Type: text/plain; charset=utf-8');
    echo $i;    
    $current_day = $date->decreaseDay($current_day);
}

Why I am battling is because importStatement.php, that output the progress, is "disconnected" or unrelated to the progress bar. The progress bar needs it's input from this script. Can someone please guide me?

Eugene van der Merwe
  • 4,390
  • 1
  • 35
  • 48

1 Answers1

0

I've implemented progress bars like this before (not sure this is the best way, but it worked for me).

Have another MySQL table to store progress.

When you start the actual report, create an entry in the 'progress' table with a job ID. Have the long running PHP script continuously update the 'progress' table rather than echoing the progress to the screen. Point your AJAX progress bar at a quick PHP script that accepts a job ID and echos the progress from the database.

Brad
  • 229
  • 2
  • 9