title: export query to excel and have a progress bar for it
body: <p>Good day </p>
  
  <p>I am currently struggling with regards to outputting a progress bar while my query is being exported to an excel file. The excel file tries to display the progress bar with the sheet instead of it being displayed on the web browser. I tried using the exit method but the code that contains the progress bar which is underneath is not read instead.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>&lt;?php connect_local_host();?&gt;
  &lt;html lang="en"&gt;
  this is for the progress bar
  &lt;body&gt;
  
  this is for the progress bar
  
  &lt;!-- Progress bar holder --&gt;
  &lt;div id="progress" style="width:500px;border:1px solid #ccc;"&gt;&lt;/div&gt;
  &lt;!-- Progress information --&gt;
  &lt;div id="information" style="width"&gt;&lt;/div&gt;
  
  this code here is for exporting the query
  &lt;?php
  
  function connect_local_host()
  {
  
  $time_start = microtime(true);
  
  $mysqlserver = "localhost";         
  $user = "root";
  $pass = "";
  $db = "Profusion";
  
  $link = mysql_connect( "$mysqlserver", $user, $pass );
  if ( ! $link )
  die( "Couldn't connect to MySQL" );
  //print "Successfully connected to server&lt;P&gt;";
  
  mysql_select_db( $db )
  or die ( "Couldn't open $db: ".mysql_error() );
  //print "Successfully selected database \"$db\"&lt;P&gt;"; 
  
  $query = "SELECT uniqueid,cdr_id from source_cdr";
  $export = mysql_query ($query ) or die ( "Sql error : " . mysql_error( ) );
  $fields = mysql_num_fields ( $export );
  
  for ( $i = 0; $i &lt; $fields; $i++ )
  {
  $header .= mysql_field_name( $export , $i ) . "\t";
  }
  
  while( $row = mysql_fetch_row( $export ) )
  {
  $line = '';
  
  foreach( $row as $value )
  {                                            
  if ( ( !isset( $value ) ) || ( $value == "" ) )
  {
      $value = "\t";
  }
  else
  {
      $value = str_replace( '"' , '""' , $value );
      $value = '"' . $value . '"' . "\t";
  }
  
  $line .= $value;
  }
  $data .= trim( $line ) . "\n";
  }
  $data = str_replace( "\r" , "" , $data );
  if ( $data == "" )
  {
  $data = "\n(0) Records Found!\n";                        
  }
  
  header("Content-type: application/octet-stream");
  
  header("Content-Disposition: attachment; filename=audit_data.xls");
  
  header("Pragma: no-cache");
  
  header("Expires: 0");
  
  print "$header\n$data";
  
  //exit();
  
  //}
  
  // this is for the total time taken to execute the export code
  $time_end = microtime(true);
  $time = $time_end - $time_start;
  $t_t=$time/$fields;
  $total = $t_tstrong text;
  // Loop through process
  
  for($i=1; $i&lt;=$total; $i++)
  {
  // Calculate the percentage
  $percent = floatval($i/$total * 100)."%";/
  
  echo round($percent,0); 
  
  // Javascript for updating the progress bar and information
  echo '&lt;script language="javascript"&gt;
  
  document.getElementById("progress").innerHTML="&lt;div style=\"width:'.$percent.';background-      color:#ddd;\"&gt;&amp;nbsp;&lt;/div&gt;";
  &lt;/script&gt;';
  
  // This is for the buffer achieve the minimum size in order to flush data
  echo str_repeat(' ',1024*64);
  
  // Send output to browser immediately
  flush();
  
  // Sleep one second so we can see the delay
  sleep(1);
  }
  
  // Tell user that the process is completed
  echo '&lt;script language="javascript"&gt;document.getElementById("information").innerHTML="Process   completed"   &lt;/script&gt;';
  
  echo '&lt;br/&gt;';
  echo  "Total time taken :"." ".round($time,6) . " s";
  }
  
  ?&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>