运行while循环时的进度条

时间:2023-04-08
本文介绍了运行while循环时的进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有这个 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.

这是如何轻松"完成的?

How is this "easily" done?

进度条的代码可以在与 while 循环相同的文档中,也可以在另一个更简单的文档中.

The code for the progress bar can either be in the same document as the while loop, or in another if easier.

推荐答案

您可以创建一个主"文件,该文件对第一个文件执行 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 the doAjax() function with the last element, we do the next iteration doAjax(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循环时的进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:PHP 将旧的 mysql_query 更改为 PDO 下一篇:PHP json_encode() 在 while 循环中

相关文章