[英]What is the best way to display a large set of data (PHP)
I have a function that connect to about a 1000+ databases and get the data back and put it in an array. 我有一个函数连接到大约1000多个数据库并获取数据并将其放入数组中。
however, the data is so large and I want to display the results on the browser each time the loop goes through a connection instead of waiting for the whole loop to finish. 但是,数据是如此之大,我希望每次循环通过连接时在浏览器上显示结果,而不是等待整个循环完成。
or if there is a better way of doing so, please share. 或者如果有更好的方法,请分享。
function Get_data()
{
// loop base on the number of available databases
for ($i = 0; $i < count($client_databases); $i++) {
// Getting connection for each database
$database = $this->connect_clients_db($client_databases[$i]);
// return an array of data from the database
$ClientShema = $this->getSomeData($database);
// put the data in array
$schemas[]=$ClientShema;
}
return $schemas;
}
example of results would be 结果的例子是
loop 1 (database_one) this is a data coming from database one loop 1(database_one)这是来自数据库1的数据
loop 2 (database_two) this is a data coming from database two loop 2(database_two)这是来自数据库2的数据
You can turn on output buffering and flush the buffer periodically to the browser. 您可以打开输出缓冲并定期刷新缓冲区到浏览器。
First, you have to send a certain amount of data to the browser: 首先,您必须向浏览器发送一定数量的数据:
echo str_repeat(" ", 40000);
Next, you need to start output buffering: 接下来,您需要启动输出缓冲:
ob_start();
Finally, after you've output what you want to send to the browser, you need to flush the output buffer. 最后,在输出要发送到浏览器的内容后,需要刷新输出缓冲区。 I found that I had to call the following three functions to get this to work:
我发现我必须调用以下三个函数才能使其工作:
ob_end_flush();
ob_flush();
flush();
So, your code might look like the following: 因此,您的代码可能如下所示:
function Get_data()
{
echo str_repeat(" ", 40000);
//loop base on the number of available databases
for ($i = 0; $i < count($client_databases); $i++) {
//Start buffering output
ob_start();
// Getting connection for each database
$database = $this->connect_clients_db($client_databases[$i]);
// return an array of data from the database
$ClientShema = $this->getSomeData($database);
// put the data in array
$schemas[]=$ClientShema;
//Write some output somewhere here.
//flush the output buffer here.
ob_end_flush();
ob_flush();
flush();
}
return $schemas;
}
Here is a block of code you can use to test this technique: 以下是可用于测试此技术的代码块:
<?php
echo str_repeat(" ", 40000);
for ($i = 0; $i < 10; $i++) {
ob_start();
echo "Hello world.<br/>";
ob_end_flush();
ob_flush();
flush();
usleep(500000);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.