本文介绍了控制器中的PHP刷新信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从PHP CodeIgniter中的控制器发送数据以进行查看。

我正在通过 ajax 并使用<$ c来调用PHP函数$ c> ob_flush 发送回数据,但问题是所有刷新调用在以后的调用中被串联。

I am trying to send data to view from controller in PHP CodeIgniter.
I am calling the PHP function through ajax and using ob_flush to send data back but the problem is that all the flush calls are concatenated in later calls.

例如:第一次刷新发送 1 第二次刷新调用发送 12 而不是 2

For example: the first flush send 1 second flush call send 12 instead of 2.

这是我的控制器循环。

foreach ($csv_array as $row) {
    ob_start();
    $varint=$varint+1;
    echo $varint;
    $content = ob_get_contents();
    ob_end_clean();
    echo $content;
    ob_flush();
    flush();
    while (ob_get_level() > 0) {
        ob_end_clean();
    }
}

我的ajax调用是这样:

My ajax call is this:

document.getElementById('upld').onclick = function() {
    xhr = new XMLHttpRequest();
    var mydata = new FormData($('#form')[0]);
    xhr.open("POST", base_url+"index.php/controller/function", true);
    xhr.onprogress = function(e) {
        console.log(e.currentTarget.responseText);
    }
    xhr.onreadystatechange = function(data='') {
        if (xhr.readyState == 4) {
            console.log(myArr);
        }
    }
    xhr.send(mydata);
};


推荐答案

说:

您可以将 flush()替换为,因为我不认为您要刷新系统输出缓冲区。

In your case you can replace flush() with ob_flush() as I don't think you are looking to flush the system output buffers.

作为此代码的补充:

$content = ob_get_contents();
ob_end_clean();

可以压缩为一个函数调用,该函数调用是前面两个命令的组合的别名:

Can be condensed into one function call that is an alias for both of the previous commands combined:

$content = ob_get_clean();

这篇关于控制器中的PHP刷新信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 01:35