1

Following is my script in which I put the progress bar to tell the user about the progress of sheets data being processed the problem is that the progress bar appears after the whole process of saving xls to csv being done kindly let me know how can i modify the following so that progress bar appears correctly right on when the current sheet is on process. Thanks,

 $excel = new Spreadsheet_Excel_Reader();
    $excel->setOutputEncoding('UTF-16');
    $excel->read('Student2.xls');
    $x=1;
    $sep = ",";

   $nbSheets = count($excel->sheets);
  echo $x." -  ".$nbSheets;

  $total = $nbSheets - 1;

for($i = 0; $i < $nbSheets; $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(1);

   ob_start();
    while($x<=$excel->sheets[$i]['numRows']) {
        $y=1;
        $row="";
        while($y<=$excel->sheets[$i]['numCols']) {
            $cell = isset($excel->sheets[$i]['cells'][$x][$y]) ? $excel->sheets[$i]['cells'][$x][$y] : '';
            $row.=($row=="")?"\"".$cell."\"":"".$sep."\"".$cell."\"";
            $y++;
        } 
        echo $row."\n"; 
        $x++;
    }
    $x = 1; $y = 1;
    $fp = fopen("data.csv",'a');
    fwrite($fp,ob_get_contents());
    fclose($fp);
    ob_end_clean();

}

echo "CSV file created successfully";
// Tell user that the process is completed
echo '<script language="javascript">document.getElementById("information").innerHTML="Process completed"</script>';
soft genic
  • 2,016
  • 3
  • 27
  • 44

3 Answers3

0

Maybe this could solve your problem :

questions#6556790 : echo-string-while-every-long-loop-iteration-flush-not-working

Also why are you using the output buffer for writing in the data.csv file ? Output buffer, as it said, is for outputting data so in this case I think it would be more logical to simply pass the $row."\n" in the fwrite function. Correct me if I'm missing something here.

Community
  • 1
  • 1
WiMantis
  • 376
  • 3
  • 11
  • Nah!! didnot worked in my case more over i cannot use in fwrite cause i am not getting single row in that – soft genic Dec 09 '12 at 09:18
  • oh ya my mistake, you would need to make an other variable called `$rows` in which you `$rows.=$row."\n";` instead of `echo $row."\n";` and then it should work without the output buffer stuff, I guess. – WiMantis Dec 09 '12 at 09:32
  • well i got into one more trouble and a wierd one no matter what i mention before the piece of code of the for loop, it runs first. seriously why :-? – soft genic Dec 09 '12 at 09:52
0

You'll need to use AJAX, or something client-server communications framework, to make this happen.

You could try looking at this example code (download the code via the link near the bottom of the article).

This SO question and this question seem to address a similar issue.

Much as I hate to say it, searching "ajax php progressbar" will likely bring up a host of examples.

Community
  • 1
  • 1
Richard
  • 56,349
  • 34
  • 180
  • 251
0

I reviewed your code and the link you mentioned. There is one difference in your code and the difference is you have commented the sleep(1);, and because of only this command you can not see the working progress bar properly.

So kindly uncomment this sleep(1); and your code will work properly.

Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100