問題描述
我有這個 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 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循環時的進度條的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!