Progress Bar in PHP – Implement a progress bar with PHP |
|
Almost every PHP developer feels the need to display a progress meter or progress bar in PHP at some point in their life when large amount of data processing is done on the server either in the form of file uploading or bulk insert/update operations of database queries are being performed however not may of them know that PHP has a big limitation that does not allow to access raw data while code execution is being done.
Good news is that we can achieve our goal using PHP’s flushing buffer to create a progress bar for large PHP applications. You can use the flush() function to push more data to the browser while the php script is running using the following code. This data being elements for small pieces of the progress bar, you can rather easily have a universal solution for all heavy scripts.
<?php
ob_end_flush();
// This is to clear the buffer and it should be called at start
?>
<html>
<head>
<title>PHP Progress Bar Example</title>
<script type="text/javascript">
<!--
function DisplayProgress(prog_value,div_id)
{
document.getElementById(div_id).innerHTML = prog_value + "% done out of 100%";
}
-->
</script>
</head>
<body>
<div id="progbar"></div>
</body>
</html>
<?php
$total_count = 100000;
for($i=0; $i<=$total_count; $i=$i+10)
{
$progress_made = ($i/$total_count)*100;
print "<script type=\"text/javascript\">
DisplayProgress(".$progress_made.",'progbar');
</script>\n";
flush();
// Push the new data to browser
}
?>
Please note that on some occasions, the webserver, proxy or the client browser can buffer data no matter what you do, so this will not work 100% for everyone at every situation. Still, this is a great idea.
|
|
|
| Posted
by: Administrator
This question has been viewed 243 times so far.
|
| Click
Here to View all the questions in Web Design & Development
category. |
File Attachments |
| There are no attachment file(s) related to this question. |
| |
User Comments |
 |
|
| There are no user comments for this question. Be the first to post a comment. Click Here |
Related Questions |
| There are no other questions related to this question. |