Page 1 of 1

How to show status

Posted: Mon Mar 31, 2008 5:40 pm
by micknc
I am not sure if this is even possible but I have written a page that formats and transfers data from one mysql table to another. Is there any script that would help me show the status of this transfer?

The table will have different numbers of rows each time and the user has no idea if it is 20 rows or 20 thousand. Any ideas?

Re: How to show status

Posted: Mon Mar 31, 2008 5:46 pm
by s.dot
You could print it out to the page.. ;p using mysql_num_rows() to determine how many rows there are

Code: Select all

$i = 0;
$numRows = mysql_num_rows($result);
while (...)
{
    echo $i++ . ' rows out of ' . $numRows . ' transferred<br />';
}

Re: How to show status

Posted: Mon Mar 31, 2008 6:01 pm
by micknc
I thought about using something like that but that is going to output a new line for every loop sequence so it would basically only be useful as long as it was on the screen (30 lines or so).
Instead of looking like this:

0 rows out of 2035 transferred
1 rows out of 2035 transferred
2 rows out of 2035 transferred
3 rows out of 2035 transferred
4 rows out of 2035 transferred
5 rows out of 2035 transferred
6 rows out of 2035 transferred
7 rows out of 2035 transferred
8 rows out of 2035 transferred
9 rows out of 2035 transferred
10 rows out of 2035 transferred
11 rows out of 2035 transferred

I am trying to keep it all in the first line. If I put it in the loop I get the above. Outside of the loop I get nothing till it is all said and done anyway...

Re: How to show status

Posted: Mon Mar 31, 2008 6:12 pm
by s.dot
Well, you could limit it.

Code: Select all

$i = 0;
while (...)
{
     if (($i % 100) == 0)
     {
         //echo rows printed
     }
}
That would print every 100 rows.
You cannot echo anything outside of the while loop until the loop is finished.

If the printing gets too long, you could use javascript to scroll to the bottom, if it is that important.

Re: How to show status

Posted: Mon Mar 31, 2008 7:14 pm
by micknc
So here is what I have to show the percentage complete based on rows:

Code: Select all

<html>
<head>
<style type="text/css"><!--
 
div {
 margin: 1px;
 height: 20px;
 padding: 1px;
 border: 1px solid #000;
 width: 275px;
 background: #fff;
 color: #000;
 float: left;
 clear: right;
 top: 38px;
 z-index: 9
}
 
.percents {
 background: #FFF;
 border: 1px solid #CCC;
 margin: 1px;
 height: 20px;
 position:absolute;
 width:275px;
 z-index:10;
 left: 10px;
 top: 38px;
 text-align: center;
}
-->
</style>
</head>
<body>
 
<?php
include "../../includes/dbconnect.php";
 
echo str_pad('Loading... ',4096)."<br />\n";
 
$result=mysql_query("select * from inv001");
$numRows = mysql_num_rows($result);
$percentage = 100/$numRows;
$d = $percentage;
$i = $percentage;
while($row=mysql_fetch_assoc($result)){
    $i = $i + $percentage;
    $d = $d + $percentage;
    $iformat = round($i);
    echo '<div class="percents">' . $iformat . '%&nbsp;complete</div>';
 
mysql_query("INSERT STATEMENT") or die(mysql_error());
}
?>
<div class="percents" style="z-index:12">Done.</div>
</body>
</html>
It is adaptation of a progress bar using the flush function but it gets the job done. Let me know if you see anything that can be improved upon.

Re: How to show status

Posted: Mon Mar 31, 2008 7:56 pm
by John Cartwright
Take a look at a script called BigDump. It takes x amount of queries then resets the page at the correct pointer (using sessions). This also helps reduce the memory load/execution time limitations.

This combined with ajax would probably be most ideal.