0

我有一个 PHP 脚本需要很长时间才能执行,因为每个操作都必须对另一个网站进行 SOAP 调用。我想添加一个进度条指示器,以便我可以看到该脚本的完成百分比。

我相信这样做的方法是执行一个异步 Ajax 调用来执行 PHP,然后再进行另一个 Ajax 调用来更新完成百分比。但是我在把它放在一起时遇到了很多问题。

到目前为止,我只有这个:

<script language='Javascript'>

$().ready(init);

function init() {       
    $.get('importStatement.php');           
}

$(function() {
$( "#progressbar" ).progressbar({
  value: 0
});
});

</script>

<div id="progressbar"></div>

我的输出完成百分比 ($i) 的 PHP 脚本:

$current_day = $date->today;
$days_to_read = 45;
// Scroll through the last $days_to_read dates and import a statement for that day
for ($i = 0; $i < $days_to_read; $i++) {
    $statement_date = $date->convertToMysql($current_day);
    $bank->importStatement($statement_date, true);
    $percent = intval($i/($days_to_read-1) *100).  "%";
    // disable caching
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
    header('Cache-Control: no-cache, must-revalidate');
    header('Pragma: no-cache');
    header('Expires: Mon, 26 Jul 1991 05:00:00 GMT');  // disable IE caching
    header('Content-Type: text/plain; charset=utf-8');
    echo $i;    
    $current_day = $date->decreaseDay($current_day);
}

我为什么要战斗是因为输出进度的 importStatement.php 是“断开连接”或与进度条无关。进度条需要从这个脚本输入。有人可以指导我吗?

4

1 回答 1

0

我之前已经实现过这样的进度条(不确定这是最好的方法,但它对我有用)。

有另一个 MySQL 表来存储进度。

当您开始实际报告时,请在“进度”表中创建一个带有作业 ID 的条目。让长时间运行的 PHP 脚本不断更新“进度”表,而不是将进度回显到屏幕上。将您的 AJAX 进度条指向一个快速 PHP 脚本,该脚本接受作业 ID 并回显数据库中的进度。

于 2013-11-30T19:34:56.747 回答