用于长时间运行 MySQL 操作的 PHP Ajax jQuery UI 进度条

标签 php jquery mysql ajax jquery-ui-progressbar

我有一个 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/

相关文章:

php - 如何使用PHP中的YouTube API在特定 channel 上载视频?

php - 如何在没有安装插件的情况下向用户的浏览器显示pdf文件

javascript - 用于 JSP/Java 的 jQuery 上传插件

php - 选择用户详细信息时的 MySQL 更新事件

MySQL从毫秒字段中选择格式化日期

php - While 循环并更新数据

php - 如何在 PHP 项目中找到未使用的函数

javascript - Jquery:无法在 iframe 中找到元素

javascript - 使用 Firefox 的 jQuery hide() 问题

mysql添加主键