PHPKB Knowledge Base Software Logo Buy Now 
PHPKB 1.5 Knowledge Base Software
Knowledge Base Management Software  
Knowledge Base Home Knowledge Base Home | Knowledge Base Glossary Glossary | Contact Us Contact Us
Home > All Categories > Computers & Information Technology > Internet and WWW > Web Design & Development > Progress Bar in PHP – Implement a progress bar with PHP
Question Title 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 File Attachments
There are no attachment file(s) related to this question.
How helpful was this article to you?
User Comments User Comments Add Comment
There are no user comments for this question. Be the first to post a comment. Click Here
Related Questions Related Questions
There are no other questions related to this question.
Article Information Additional Information
Article Number: 607
Created: 2008-05-09 3:57 AM
Rating: 5 out of 5.
Rate Bar: 5 Stars
 
Article Options Article Options
Print Article Print this Article
Email Article to Someone
Export to Adobe PDF Export to PDF File
Export to MS Word Export to MS Word
Bookmark Article
del.icio.us Bookmark del.icio.us Bookmark
Reditt Bookmark Reditt Bookmark
Digg Bookmark Digg Bookmark
Subscribe to Article
 
Search Knowledge Base Search Knowledge Base
 
 

Powered by PHPKB Knowledge Base Software