Good day

I need help incorporating a progress bar into my code, I first needed to export a query from my database but however the time taken for the query to be exported to a xls file should be shown in a form of a progress bar. if i exit from the export code after exporting to an xls file, the progress bar code is not read at all but if i do not exit from the export to xls file code the code tries to output the progress bar into the excel file which it shouldn't do.

Here is the code

<?php connect_local_host();?>

<html lang="en">

this is for the progress bar
<body>
<!-- Progress bar holder -->
<div id="progress" style="width:500px;border:1px solid #ccc;"></div>
<!-- Progress information -->
<div id="information" style="width"></div>

this code here is for exporting the query
<?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<P>";

mysql_select_db( $db )
or die ( "Couldn't open $db: ".mysql_error() );
//print "Successfully selected database \"$db\"<P>"; 


$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 < $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();
//}

// progress bar code again
$time_end = microtime(true);
$time = $time_end - $time_start;
$t_t=$time/$fields;

$total = $t_t + 1;// added 1 to verify that the progress bar does work
// Loop through process
for($i=1; $i<=$total; $i++){
// Calculate the percentage
$percent = floatval($i/$total * 100)."%";/

echo round($percent,0); 

// Javascript for updating the progress bar and information
echo '<script language="javascript">
document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.';background-     color:#ddd;\">&nbsp;</div>";
</script>';


// 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 '<script language="javascript">document.getElementById("information").innerHTML="Process   completed"</script>';

echo '<br/>';
echo  "Total time taken :"." ".round($time,6) . " s";

}


 ?>

</body>
</html>

my problem is where the exit command is after the export code is executed I need to incorporate my progress bar on the web browser instead of implementing it in the excel file.

Your help will be highly appreciated.

Recommended Answers

All 4 Replies

okay nauticalmac thanks alot, what i really want is to exit from the query exporting code and start reading the progress bar code so that I can get the time taken to do the whole export of the query to the excel file sheet.

Disclaimer:I have not implemented anything like this.

The way your code is written, the php will have completed by the time the html below it is run.
1. You need to run your query asynchronously to the web page so the web page can monitor the progress. For example put your code for exporting the query into a separate 'export' script run by an ajax call. After making the call, your web page is running in parallel to the query.
2. On a regular basis make additional ajax calls to a separate 'progress' script that monitors the progress. Your export query script could keep on writing the number of rows to a file and the progress script read the file and return the number to the web page which would update the progress bar. You want to avoid slowing the export down too much, this defeats the purpose. E.g. for a big file, write for every 100 rows.
3. NOTE: your ajax 'export' script can not download the file directly. You will have to do this from your web page. The 'export' script could save the file to disk and when it returns success to the webpage use javescript to navigate to a new page which downloads the saved file.

I hope this gives you some ideas for a solution.

okay nauticalmac sorry for the late reply thanks for the help, I'll work around it and see if I wouls come up with a solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.