Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9128171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:27:10+00:00 2026-06-17T07:27:10+00:00

I have this while loop, that basically loops through a lot of records in

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:27:12+00:00Added an answer on June 17, 2026 at 7:27 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one thread that is receiving data over a socket like this: while
I have created this function to check abecedarian with while loop (A word is
I have a function creating entries via grid() like this: (replace while-loop with function,
I have an nsthread, a while loop within this one. It's getting objects from
I have this little question that's been on my mind for a while now.
I have part of my script that goes like this: while read line do
I have this foreach statement that is supposed to cycle through an array, but
Not sure why this is occurring: Basically, I have an array that contains the
We have a loop in SQL Server 2005 that loops around on a table
This is basically the picture: I have a server and a client (operating through

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.