php - Progress bar while running while loop -


i have while loop, loops through lot of records in database, , inserts 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 easy reading - correct 1 has longer array)

i more graphical, , show progress bar on how far has went, instead of seeing page loading few minutes (there ~20.000 rows in 1 - have tables more data)

i total number old database, , put current number variable 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 need fetch $i , display - or it.

how "easily" done?

the code progress bar can either in same document while loop, or in if easier.

you can "master" file ajax first file run single query. entry id's in master file, , pass parameter second file single query. store these ids in javascript array.

create function this, , when first ajax done, move second element of id array, , ajax second parameter. that's how magento imports done way :)

if need further explanations, let me know, tried best explain, may have not been clear.

// generate javascript array using php. // let's have ids have processed in $ids php array. ids = [<?php echo implode(',', $ids); ?>];  function doajax(i) {     $.ajax({  // using jquery simplicity         'url': "ajax.php?id=" + ids[i],     }).done(function(){         if ( >= 0 ) {             // @ point know you're @ ((ids.length-i)/(ids.length) * 100) percent of script             // can this:             // $('.progressbar').css('width', ((ids.length-i)/(ids.length) * 100) + '%');             doajax(i-1);         }     }); }  doajax(ids.length); // starting last entry 

so, explain does. starts declaring global javascript array has ids need changed.

then declare recursive ajax function, way can make sure 1 ajax runs @ single time (so server doesn't blow up), , can have accurate progress. ajax function following:

  • sends request ajax.php?id=xxx - xxx 1 of ids in javascript array.
  • in file, id ($_get['id']), take old database, , insert in new one. 1 entry.
  • when ajax done, goes done() function. since start doajax() function last element, next iteration doajax(i-1). since we're going backwards in array, check if key positive. if it's not, script stop.

that's it.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -