php服务器处理数据库中ajax的流式处理进度 [英] streaming progress in ajax from php server processing database

查看:115
本文介绍了php服务器处理数据库中ajax的流式处理进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在该网站上查看过ajax进度栏解决方案,但是我找不到任何可以帮助以下的内容,

I've Looked at ajax progress bar solutions on this site, However i can't find anything to help with the below,

基本上,
[浏览器] ---->对[运行php的服务器]进行ajax调用


我的php代码


my php code

<?php

 //input and process ajax request

 //[MySQL] processing 5000 rows of a table

 //after 5000/5000 row, end of ajax call

?>

现在,我需要向用户显示正在处理的行的状态
例如 Processing :::::::::: 2322/5000.行

Now, I need to show the user the status of the rows being processed
eg Processing::::::::: 2322/5000. row

用于处理Mysql的代码如下

The code for processing Mysql looks like this

foreach($result_set as $table_row)
{
   // process each table row
}

我如何通过xmlhttprequest让客户端知道此进度?不能使用 JQUERY ,只能使用 JavaScript

How am i going to let the client know of this progress via xmlhttprequest? cannot use JQUERY, only Javascript

尝试实施以下

服务器不断循环回显进度,

the server continously echos the progress in a loop,

<?php

 //progress

     while($i < $total_progress)
     {
     //php processing

     if(@ob_get_contents()){ ob_end_flush(); flush(); }

     echo $i; // progress
     $i++;


     }
 ?>

连续打印数据(变量$ i)并将其推入响应中,
在ajax调用结束之前,是否有一种方法可以始终从服务器读取打印的变量?

The Data (variable $i) is continously being printed and pushed into the response,
Is there a way i can read the printed variable from the server,constantly before the ajax call ends?

推荐答案

这可以结合使用AJAX,会话和javascript来完成.我从未制作过任何图形化的条形图,但过去我曾做过基于文本的条形图.

This can be done with a combination of AJAX, sessions, and javascript. I've never made any graphical versions of a bar, but I have made text-based ones in the past.

在您的客户端页面中,让AJAX调用您的页面以进行表处理.让该服务器端页面开始一个会话并创建一个会话变量以跟踪您的进度(例如 $ _ SESSION ['processedRows'] ).处理完一行后,现在进入处理循环,请增加会话var的值,然后调用 session_write_close().这会将您的会话信息写入磁盘,然后关闭该会话.正如我将在稍后解释的那样,这一点很重要.您还必须在 foreach 循环的下一次迭代中更新会话变量之前,再次调用 session_start().

In your client side page, have AJAX make a call to your page that will do the table processing. Have this server side page start a session and create a session variable to track what your progress is at (e.g. $_SESSION['processedRows']). Now inside your processing loop after you processed a row, increase the value of your session var and then make a call to session_write_close(). This will write your session info to disk and then close the session. This is important, as I will explain in a minute. You must also make another call to session_start() again prior to updating your session variable in the next iteration of your foreach loop.

回到客户端,您将有一个间隔发送另一个AJAX请求(为此,我在javascript中使用 setInterval()),这将调用另一个服务器端页面,该页面将打开一个会话并返回 $ _ SESSION ['processedRows'] 的值.您可以使用此值,并使用它来更新计数器,进度栏等.您可以返回多个值,例如JSON或HTML代码段,这无关紧要.只需确保已使用某种回调方法即可终止在客户端设置的间隔.在长时间运行的脚本中调用 session_write_close()很重要.如果您不这样做,那么用于检查进度的并发脚本将无法访问您的会话变量,并且会在等待处理脚本结束时滞后.原因是在您调用 session_write_close()之前,会话文件尚未释放.

Back on the client side, you will have another AJAX request being sent on an interval (I use setInterval() in javascript for this) which will call up another server side page that will open a session and return the value of $_SESSION['processedRows']. You can take this value and use it update your counter, progress bar, etc. You could return several values as JSON or an HTML snippet, doesn't matter. Just make sure that you have a callback method of some kind in place that will kill the interval that has been setup on the client side. It is important that you call session_write_close() in your long running script. If you do not then your concurrent script to check the progress will not be able to access your session variables and will lag while it waits for the processing script to end. The reason is that the session file has not been released until you call session_write_close().

这是非常的基础,没有多余的装饰示例.这与错误检查,超时等没有太大关系.但是它应该说明您想要做的事情.根据您的疑问,也没有jQuery.为此,您将需要三个文件.首先,您需要长时间运行的脚本,该脚本基本上使PHP短暂睡眠了5000次.这是为了说明您的表操作.

Here is a very basic, no frills example of what you are looking for. This doesn't have much in regards to error checking, timeouts, etc. But it should illustrate what you want done. No jQuery either, as per you question. For this you will need three files. First you need your long running script, this one basically puts PHP to sleep very briefly 5000 times. It is to illustrate your table operations.

startCounter.php

<?php

//Start a long-running process

$goal = 5000;
session_start();
for($i = 0; $i < $goal; $i++) {
    session_start();  //Reopen session to continue updating the progress
    if(!isset($_SESSION['progress'])) {
        $_SESSION['progress'] = $i;
        $_SESSION['goal'] = $goal;
    }
    //wait a wink
    usleep(rand(500, 2000));
    $_SESSION['progress'] = $i;
    session_write_close();  //Close session to gain access to progress from update script
    header_remove();
}

?>

接下来,您需要一个更新脚本,这个脚本非常简单,它将仅输出带有进度更新的字符串.

Next you need an update script, this one is very simple, it will only output a string with the progress update.

updateCounter.php

<?php

//Get the progress

session_start();

print("Processed " . $_SESSION['progress'] . " of " . $_SESSION['goal'] . " rows.");

?>

接下来,您需要准备好与AJAX一起使用的客户端.您需要有一种方法来启动长时间运行的脚本,然后在该请求未完成时,设置和间隔以使服务器停顿以获取当前进度.只要确保您在操作完成后取消间隔即可.这是测试页:

Next you need your client side ready to go with AJAX. You need to have a way to start the long running script, and then while that request is incomplete, you set and interval to hit the server up for the current progress. Just make sure you kill the interval when operation is done. Here is the test page:

progress.html

<!DOCTYPE html>
<html>
<head>
    <title>Progress Test</title>

    <script language="javascript">

    function go()
    {
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "startCounter.php", true);
        interval = window.setInterval( function () {
            update();
        }, 200);
        xhr.onreadystatechange = function () {
            if(xhr.readyState == 4) {
                window.clearInterval(interval);
                //Extremely likely that the AJAX update function won't make it in time to catch
                //the script at 5000 before it wins.  So let's display a message instead showing
                //that everything is done.
                document.getElementById("updateme").innerHTML = "Operation has completed.";
            }
        }
        xhr.send();
    }

    function update()
    {
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if(xhr.readyState == 4 && xhr.status == 200) {
                document.getElementById("updateme").innerHTML = xhr.responseText;
            }
        }
        xhr.open("GET", "updateCounter.php", true);
        xhr.send();
    }

    </script>

</head>

<body>

    <form>
        <input type="button" onclick="go()" value="Go!" />
    </form>

    <h3>AJAX update test</h3>

    <h4 id="updateme"></h4>

</body>
</html>

希望您能朝正确的方向前进.

Hope that gets you going in the right direction.

这篇关于php服务器处理数据库中ajax的流式处理进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆