Jump to content

how to print and keep replacing a counting variable


webent

Recommended Posts

Hi, I have a php script that inserts and updates thousands of records from a csv file into a mysql db... in the loop I have a counter that shows how many entries were made... the problem is that sometimes it can take up to 5 to 10 minutes to insert a single file, so I wanted to be able to show what the counter was on, on the page, so the user don't think there is a problem and try to close the page or something... but as you can imagine, just echoing the counter variable doesn't work so hot, since it just prints one number after another... i.e.

echo $products_inserted_counter;

 

So is there a way to replace the last number with the current number, continuously?

Link to comment
Share on other sites

echo $products_inserted_counter++;

 

uh...no.

 

 

You need a clientside language to do that, like javascript.  Look into AJAX, which is a method of mixing javascript with php.  There are plenty of tutorials out there for "loader" screens, which in principle is the same thing as what you want.

Link to comment
Share on other sites

yeah, tried that, that doesn't work, does the same thing, just continues on across the screen, adding one number after the next...

 

I was thinking about perhaps creating a table just for the counter, every pass it makes through the loop, update the table with the new count...

 

meanwhile as soon as insertion loop starts, perhaps popup a small window that refreshes every second with a query of the counter table, showing the latest count...

 

Any better suggestions would be greatly appreciated...

Link to comment
Share on other sites

Thought about and tried Javascript/AJAX ... still run into a different problem, once you pass the counter variable, that's it... I can't find a way to keep the variable feeding steadily into it...

Link to comment
Share on other sites

well, here is one bit that I tried...

function importMeter($products_inserted_counter) {
alert('products_inserted_counter' + ' Products have been inserted so far.');
}

 

That was a nightmare... had to cntrl/alt/delete out of it... LOL

Link to comment
Share on other sites

I'd have the Ajax page poll a separate PHP page every 30 seconds, and have the CVS php page store the count in a database every 100 lines, with a unique request number, and the clean it up after the script is over.  The page called by Ajax would just retrieve the value from the DB.

Link to comment
Share on other sites

yeah, tried that, that doesn't work, does the same thing, just continues on across the screen, adding one number after the next...

 

I was thinking about perhaps creating a table just for the counter, every pass it makes through the loop, update the table with the new count...

 

meanwhile as soon as insertion loop starts, perhaps popup a small window that refreshes every second with a query of the counter table, showing the latest count...

 

Any better suggestions would be greatly appreciated...

 

Not going to work. PHP is a server side language.  It's all processed first and then sent to the client.  Best you can do on that count is to make a script utilizing your db like you said, but instead of looping through everything you do it one at a time and that's it, but check the db each refresh with a meta refresh every second or so and do a new query based on that.  Throw on a condition before all that to see where the number is in that extra column and stop when it reaches.  It sounds terribly complicated and inefficient because it is. 

 

Go the AJAX method.  Google AJAX loader/percent bars, you know, the ones you see going from 0-100% to show something is loading.  You would base the % off of what iteration you're at vs. how many total.  Just have an updated number instead of a load bar.  Or you can have a load bar that fills up, because it would look more flashy and cool and it might score you a date with that hot chick in accounting.

Link to comment
Share on other sites

yeah, tried that, that doesn't work, does the same thing, just continues on across the screen, adding one number after the next...

 

I was thinking about perhaps creating a table just for the counter, every pass it makes through the loop, update the table with the new count...

 

meanwhile as soon as insertion loop starts, perhaps popup a small window that refreshes every second with a query of the counter table, showing the latest count...

 

Any better suggestions would be greatly appreciated...

 

Not going to work. PHP is a server side language.  It's all processed first and then sent to the client.  Best you can do on that count is to make a script utilizing your db like you said, but instead of looping through everything you do it one at a time and that's it, but check the db each refresh with a meta refresh every second or so and do a new query based on that.  Throw on a condition before all that to see where the number is in that extra column and stop when it reaches.  It sounds terribly complicated and inefficient because it is. 

 

Go the AJAX method.  Google AJAX loader/percent bars, you know, the ones you see going from 0-100% to show something is loading.  You would base the % off of what iteration you're at vs. how many total.  Just have an updated number instead of a load bar.  Or you can have a load bar that fills up, because it would look more flashy and cool and it might score you a date with that hot chick in accounting.

 

"Hey, I noticed you had a load bar....I just want to let you know that I think that's really hot."

 

Lol, I want to meet the person who actually says that so I can laugh in their face.

Link to comment
Share on other sites

Ok, I'll do some more research on the AJAX loader bar... I did previous when I was looking for a loader bar for the file upload section, it seemed horribly complicated, there didn't seem like any solution that didn't involve server reconfiguration... although I have root, I am very timid about using it...

Link to comment
Share on other sites

So like what I was saying? Just not quite so often, not every second, more like only feed every hundredth count and only query every 30 seconds...

 

Well, no...the way I said it was slightly different.  There wouldn't be a giant loop looping through everything.  Your way won't work because that popup that which you speak of won't actually popup until the php is completely done executing, in other words, at the end of the loop.

Link to comment
Share on other sites

Ok, I'll do some more research on the AJAX loader bar... I did previous when I was looking for a loader bar for the file upload section, it seemed horribly complicated, there didn't seem like any solution that didn't involve server reconfiguration... although I have root, I am very timid about using it...

 

You're right.  The basic setup to do a small xhttprequest or whatever that darn thing is called looks very scary at first sight, but if you take deep relaxing breaths, clear your mind and then give it a proper look, there's really not much to it.  99% of it is just a couple of functions that set it all up and you don't really have to do anything with it at all. 

Link to comment
Share on other sites

Alright, cool, I will try to get together my Zen :) and make an attempt at it...

 

Just an after thought, hopefully once I install these files and get the configuration right, it will work for any site on the server using the program, kind of like the Smarty that I installed...

 

Like the Smarty though, I will have to remember how to install and do it on each server... ack!

Link to comment
Share on other sites

Lol, I want to meet the person who actually says that so I can laugh in their face.

 

You might have trouble getting inside somebody's face :-\

 

You could always do like "Jay and Silent Bob Strike Back" and track them down... :)

Link to comment
Share on other sites

I have done a simular thing with mySQL replication and displaying each table that has been replicated.

 

The best way to do it..Is use Ajax with starting numbers..

Id suggest to find do an initial query to find the total number of rows to be added..

then use ajax to send a $start var to the php page..

after the $start figure has been reached the script will exit and return information to the Javascript which can then determine the % left and continue on with another Ajax request until the script has finished all the rows.

 

Thats my 2 cents  ;)

Link to comment
Share on other sites

Since I'm going to have to ultimately end up having to use the AJAX solution, which is a bit complicated... I just made a temporary solution for it, until I can come back to it later...

 

Here is what I've come up with so far...

 

In the function page, I have this...

$total_count = 0;
$handle = fopen($_POST['file_name'], "r");
while (($fields = fgetcsv($handle, 0, ",")) !== FALSE) {
$total_count++;
}
$total_count -= 1;
$count_results = mysql_query("UPDATE insertion_counter SET total_count = '$total_count' WHERE counter_id = '1'");  
/////////// End getting total amount of rows in csv file

$date_time_stamp = date("Y-m-d h:i:s");
$empty_model_no = 0;
$row_counter = 0;
$handle = fopen($_POST['file_name'], "r");
while (($fields = fgetcsv($handle, 0, ",")) !== FALSE) { 
    $row_counter++;
    if ($row_counter == 1) { // First row header removed. 
                                }    else    {

$products_inserted_counter = $row_counter - 1;   
$count_results = mysql_query("UPDATE insertion_counter SET current_count = '$products_inserted_counter' WHERE counter_id = '1'"); 

 

Then on the page that is calling the function page, I open a small iframe without borders or scrollbars...

<iframe name="COUNTERFRAME" src="import-status.php" width="150" height="50" scrolling="no" frameborder="0"></iframe>

 

Then in the import-status.php page that is being called into the iframe, I use this...

<?
if ($percent_complete != 100) {
echo '<meta http-equiv="refresh" content="15">'; 
}
include_once "connection.php";
$results = mysql_query("SELECT current_count, total_count FROM insertion_counter WHERE counter_id = '1'");
    while ($row = mysql_fetch_array($results)) {
        $current_count = $row[current_count];
        $total_count = $row[total_count];
    }
if ($total_count != 0) {
$percent_complete = number_format($current_count / $total_count,2);
$percent_complete *= 100;
}
if ($percent_complete != 0) {
echo '<center>Loading in Progress<br>' . $percent_complete . '% complete</center>';
}
if ($percent_complete == 100) {
$count_results = mysql_query("UPDATE insertion_counter SET current_count = '0', total_count = '0' WHERE counter_id = '1'");
} 
  
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.