PHP and jQuery progress bar while parsing

17,222

Solution 1

I found another way to do it, this is to help all of those people that have the same question. I found it out on here http://w3shaman.com/article/php-progress-bar-script

<?php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    <title>Progress Bar</title>
</head>
<body>
<!-- Progress bar holder -->
<div id="progress" style="width:500px;border:1px solid #ccc;"></div>
<!-- Progress information -->
<div id="information" style="width"></div>
<?php
// Total processes
$total = 10;
// Loop through process
for($i=1; $i<=$total; $i++){
    // Calculate the percentation
    $percent = intval($i/$total * 100)."%";

    // 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>";
    document.getElementById("information").innerHTML="'.$i.' row(s) processed.";
    </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>';
?>
</body>
</html>
?>

Solution 2

Executing length php process while showing progress, even having multiple progress bars on same page and pre/post hooks: http://pastebin.com/KSxjC01r

I've done it for myself, because I need it.

Feel free to use it.

Share:
17,222

Related videos on Youtube

Username
Author by

Username

I like technology.

Updated on July 04, 2022

Comments

  • Username
    Username almost 2 years

    I was wondering how I could go about making a progress bar for parsing HTML data. Essentially the user searches something and I parse another website. I tried doing it with getting the amount of objects found in an array then dividing 100 by it, and getting the current one in a for loop and multiplying it by 100/total. Then I would update a text file with the value. Then update the progress bar with that value. However, I want a more efficient way to do it. Also it has to be unique to each user. Thanks.

  • Username
    Username almost 11 years
    Well, the parsing process was built with PHP. Using the class simple_html_dom
  • Krasimir
    Krasimir almost 11 years
    In PHP you can't access already started script. During its execution you can only programatically leave markers in database or file. Then you can read them with another php script, but that sounds really bad idea. In general you have javascript API for getting infromation about uploading of files, but not for script execution.
  • Username
    Username almost 11 years
    So your saying that it is NOT a good idea to write to a file in the PHP while loop?
  • Krasimir
    Krasimir almost 11 years
    Yes. You should not rely on a file writing. Even if you use different files for the different users, which will make your site really heavy. Also will slow down the whole process.
  • Username
    Username almost 11 years
    Okay, what about writing to a cookie?
  • Krasimir
    Krasimir almost 11 years
    I'm not sure, but I think that you should reload the page to set the cookie value. In your case, you are running php script and you can't reload it till it parses the whole html.
  • Username
    Username almost 11 years
    So your saying the nodejs thing is the only option. I've never heard of nodejs, do I have to rewrite my whole parser?
  • Krasimir
    Krasimir almost 11 years
    Probably nodejs is not the only one option, but yes you should probably write your parse. However you may use jquery there and make your life easier. To work with jquery in the backend and parse the html will be much much faster and I guess that you don't even need a preloader. Just working and done. Using nodejs also means that your hosting provided should support that.
  • Mohsen Haeri
    Mohsen Haeri almost 3 years
    Error 404 on the link