allow PHP script with long execution time to send updates back to the browser
<p>I looked over a few of the questions, namely</p>

<ul>
<li><a href="https://stackoverflow.com/questions/7049303/show-progress-for-php-long-script">Show progress for long running PHP script</a></li>
<li><a href="https://stackoverflow.com/questions/2996088/how-do-you-run-a-long-php-script-and-keep-sending-updates-to-the-browser-via-htt">How do you run a long PHP script and keep sending updates to the browser via HTTP?</a></li>
</ul>

<p>and neither one seems to answer my question, part of which seems to be "how do I do this?" and the other half is "Hey, the way I'm doing this right now - is it the best way?  Could I code this better?"</p>

<p>I have a simple ajax script that sends some data over to a PHP script:</p>

<pre><code>$.ajax({
    type: 'POST',
    url: 'analysis.php',      
    data: { reportID:reportID, type:type, value:value, filter_type:filter_type, filter_value:filter_value, year:year },        
    success:function(dataReturn){          
      analysis_data = JSON.parse(dataReturn);
      /* do stuff with analysis_data... */
});
</code></pre>

<p>This PHP script takes about 3 minutes to run, as it loops through a database and runs some pretty complex queries:</p>

<pre><code>&lt;?php
session_start();
ob_start();
ini_set('max_execution_time', 180);

$breaks = [ 1000, 2000, 4000, 6000, 8000, 10000, 20000, 50000, 99999999 ];
$breaks_length = count($breaks);
$p = 0;

foreach ( $breaks as $b ) {

  $p++;
  $percentage_complete = number_format($p / $breaks_length,2) . "%";

  $sql = "query that takes about 20 seconds to run each loop of $b....";  

  $query = odbc_exec($conn, $sql);
  while(odbc_fetch_row($query)){   
    $count = odbc_result($query, 'count');
  }

  $w[]  = $count;

  /* tried this... doesn't work as it screws up the AJAX handler success which expects JSON
  echo $percentage_complete;
  ob_end_flush();
  */
}

echo json_encode($w);
?&gt;
</code></pre>

<p>All of this works - but what I'd really like to do is find a way after each <code>foreach</code> loop, to output <code>$percentage_complete</code> back to the user so they can see it working, instead of just sitting there for 2 minutes with a FontAwesome icon spinning in front of them.  I tried using <code>ob_start();</code>, but not only does it not output anything until the page is done running, it echoes the value, which is then part of what is sent back to my AJAX success handler, causing it to screw up.  (I need the output in a JSON_encoded format as I use it for something else later.)</p>

<p>So far in threads I've read, my only thought is to start the <code>$breaks</code> array loop on the previous page, so instead of looping 6 times on the same page, I loop once, return an answer, then call <code>analysis.php</code> again using the second element of the <code>$breaks</code> array, but I'm not sure this is the best way to go about things.</p>

<p>Also - during the 3 minutes that the user is waiting for this script to execute, they cannot do anything else on the page, so they just have to sit and wait.  I'm sure there's a way to get this script to execute in such a way it doesn't "lock down" the rest of the server for the user, but everything I've searched for in Google doesn't give me a good answer for this as I'm not sure exactly what to search for...</p>