在while循环中运行时的进度条 [英] Progress bar while running while loop
问题描述
我有一个while循环,它基本上循环遍历数据库中的许多记录,并将数据插入另一个数据库中:
I have this while loop, that basically loops through a lot of records in a database, and inserts the data in another:
$q = $con1->query($users1) or die(print_r($con2->errorInfo(),1));
while($row = $q->fetch(PDO::FETCH_ASSOC)){
$q = $con2->prepare($users2);
$q->execute(array($row['id'], $row['username'])) or die(print_r($con2-errorInfo(),1));
}
(该脚本已缩短,易于阅读-正确的脚本具有更长的数组)
我想做更多的图形化操作,并显示进度条,而不是仅仅看到几分钟的页面加载(此行有〜20.000行-我的表有很多更多数据)
I would like to do this more graphical, and show a progress bar on how far it has went, instead of just seeing a page loading for a few minutes (there are ~20.000 rows in this one - I have tables with much more data)
我知道您可以从旧数据库中获得总数,并且我也可以轻松地将当前数字放入这样的变量中:
I get that you could get the total number from the old database, and I could also easily put the current number into a variable like this:
$q = $con1->query($users1) or die(print_r($con2->errorInfo(),1));
$i = 0;
while($row = $q->fetch(PDO::FETCH_ASSOC)){
$q = $con2->prepare($users2);
$q->execute(array($row['id'], $row['username'])) or die(print_r($con2-errorInfo(),1));
$i++;
}
但是现在我需要实际获取$i
并显示它-或类似的东西.
But now I need to actually fetch $i
and display it - or something like it.
如何轻松"完成?
进度条的代码可以与while循环位于同一文档中,如果方便,也可以位于另一文档中.
The code for the progress bar can either be in the same document as the while loop, or in another if easier.
推荐答案
您可以执行一个"master"文件,该文件对此第一个文件进行ajax运算以运行单个查询.您可以在此主文件中获取所有条目ID,然后将其作为参数传递给执行单个查询的第二个文件.将这些ID存储在javascript数组中.
You can do a "master" file that does an ajax to this first file to run a single query. You could get all the entry id's in this master file, and then pass it as a parameter to the second file that does a single query. Store these ids in a javascript array.
创建一个执行此操作的函数,完成第一个ajax时,移至id数组的第二个元素,并使用第二个参数执行另一个ajax.这就是magento导入的方式:)
Create a function that does this, and when the first ajax is done, move to the second element of the id array, and do another ajax with a second parameter. That's how magento imports are done by the way :)
如果您需要进一步的说明,请告诉我,我已尽力解释,但可能还不够清楚.
If you need further explanations, let me know, I tried my best to explain, but may have not been perfectly clear.
// you generate this javascript array using php.
// let's say you have all the ids that have to be processed in $Ids php array.
Ids = [<?php echo implode(',', $Ids); ?>];
function doAjax(i) {
$.ajax({ // using jquery for simplicity
'url': "ajax.php?id=" + Ids[i],
}).done(function(){
if ( i >= 0 ) {
// at the point you know you're at ((Ids.length-i)/(Ids.length) * 100) percent of the script
// so you can do something like this:
// $('.progressbar').css('width', ((Ids.length-i)/(Ids.length) * 100) + '%');
doAjax(i-1);
}
});
}
doAjax(Ids.length); // starting from the last entry
因此,仅解释一下它的作用.首先声明一个全局javascript数组,其中包含所有需要更改的ID.
So, just to explain what this does. It starts by declaring a global javascript array that has all the ids that will need to be changed.
然后,我声明一个递归ajax函数,这样我们可以确保在任何一次都只运行一个ajax(这样服务器就不会崩溃),并且可以取得相当准确的进度.此ajax函数执行以下操作:
Then I declare a recursive ajax function, this way we can make sure that only one ajax runs at any single time (so the server doesn't blow up), and we can have a fairly accurate progress. This ajax function does the following:
- 向ajax.php?id = xxx发送请求-其中xxx是javascript数组中的ID之一.
- 在文件中,我们获得ID(
$_GET['id']
),将其从旧数据库中取出,然后将其插入新数据库中.这仅适用于一个条目. - 完成ajax后,它将转到
done()
函数.由于我们从最后一个元素开始doAjax()
函数,因此我们进行了下一个迭代doAjax(i-1)
.由于我们要在数组中向后移动,因此我们检查键是否为正.如果不是,脚本将停止.
- Sends a request to ajax.php?id=xxx - where xxx is one of the ids in the javascript array.
- In the file, we get the id (
$_GET['id']
), you take it from the old database, and insert it in the new one. This is only for one entry. - when the ajax is done, it goes to the
done()
function. Since we start thedoAjax()
function with the last element, we do the next iterationdoAjax(i-1)
. Since we're going backwards in the array, we check if the key is positive. If it's not, the script will stop.
就是这样.
这篇关于在while循环中运行时的进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!