Progress bar while running while loop

前端 未结 2 1829
青春惊慌失措
青春惊慌失措 2020-12-21 09:38

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(pr         


        
相关标签:
2条回答
  • 2020-12-21 10:00

    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.

    0 讨论(0)
  • 2020-12-21 10:01

    You can't. The php is first interpreted by the server and then send to the user as HTML-Code.
    The only possibility would be creating a html-page and call the php-script with AJAX.

    0 讨论(0)
提交回复
热议问题