我有一个 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>
我的 PHP 脚本输出完成百分比 ($i):
$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 已“断开连接”或与进度条无关。进度条需要来自该脚本的输入。有人可以指导我吗?
最佳答案
我之前已经实现过这样的进度条(不确定这是最好的方法,但它对我有用)。
有另一个 MySQL 表来存储进度。
当您启动实际报告时,在“进度”表中创建一个带有作业 ID 的条目。让长时间运行的 PHP 脚本不断更新“进度”表,而不是将进度回显到屏幕上。将 AJAX 进度条指向一个快速 PHP 脚本,该脚本接受作业 ID 并回显数据库中的进度。
关于用于长时间运行 MySQL 操作的 PHP Ajax jQuery UI 进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20304081/