【问题标题】:Why progress bar appear after the for loop completely processed为什么for循环完全处理后会出现进度条
【发布时间】:2012-12-09 08:20:22
【问题描述】:

以下是我的脚本,我在其中放置进度条来告诉用户正在处理的工作表数据的进度问题是进度条出现在将 xls 保存到 csv 的整个过程完成后请告诉我如何我可以修改以下内容,以便在当前工作表正在处理时正确显示进度条。谢谢,

 $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>';

【问题讨论】:

  • 您必须为此使用 AJAX。
  • @YogeshSuthar 请看一下stuff.w3shaman.com/php-progress-bar 这是工作示例。
  • 请不要使用language="javascript"。这是无效的。
  • 上面的链接我提到它包含与上面相同的for循环,唯一的区别是上面的代码包含转储数据的脚本而我提到的链接没有。

标签: php javascript


【解决方案1】:

也许这可以解决你的问题:

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

另外你为什么使用输出缓冲区写入data.csv 文件?正如它所说,输出缓冲区用于输出数据,所以在这种情况下,我认为在fwrite 函数中简单地传递$row."\n" 会更合乎逻辑。如果我在这里遗漏了什么,请纠正我。

【讨论】:

  • 不!在我的情况下没有工作超过我不能在 fwrite 中使用因为我没有得到单行
  • 哦,我的错误,您需要创建一个名为$rows 的其他变量,其中您使用$rows.=$row."\n"; 而不是echo $row."\n";,然后它应该可以在没有输出缓冲区的情况下工作,我猜。
  • 好吧,无论我在 for 循环的代码之前提到什么,我都遇到了另一个麻烦和一个奇怪的问题,它首先运行。说真的为什么:-?
【解决方案2】:

您需要使用 AJAX 或其他客户端-服务器通信框架来实现这一点。

您可以尝试查看this example code(通过文章底部附近的链接下载代码)。

This SO questionthis question 似乎解决了类似的问题。

虽然我不想这么说,但搜索“ajax php 进度条”可能会出现大量示例。

【讨论】:

  • 我使用了 和 alert("hi") 甚至 hi 出现在 for 循环执行后为什么会这样..
【解决方案3】:

我查看了您的代码和您提到的链接。您的代码有一个不同之处,不同之处在于您注释了sleep(1);,并且由于仅此命令您无法正常看到工作进度条。

因此请取消注释此sleep(1);,您的代码将正常运行。

【讨论】:

  • 我使用了&lt;body onLoad="run();"&gt;alert("hi"),甚至在执行for循环后出现了hi,为什么会这样..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-01
  • 1970-01-01
  • 2011-03-07
  • 1970-01-01
  • 2010-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多