Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

php - Progress bar while running while loop

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));
}

(The script has been shortened for easy reading - the correct one has a much longer array)

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++;
}

But now I need to actually fetch $i and display it - or something like it.

How is this "easily" done?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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.

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:

  • 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.

That's about it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...