I'm trying to show function status as its looping but delete the previous status before the next one shows. Current code:
<?php
@ini_set('zlib.output_compression',0);
@ini_set('implicit_flush',1);
@ob_end_clean();
set_time_limit(0);
// Perform 1st function here
echo "Retrieving Data...";
echo str_repeat(' ',1024*64);
sleep(1);
// Perform 2nd function here
echo "Analyzing Data...";
echo str_repeat(' ',1024*64);
sleep(1);
// Perform 3rd function here
echo "Done...";
echo str_repeat(' ',1024*64);
sleep(1);
// Clean all echos here..
?>
<html>
<head>
// Dynamic head content as a result of the php functions above
</head>
<body>
</body>
</head>
Now this works, but displays all the echos one after the other. I'd like the next status to replace the first, until the end, then remove "Done" before the html is displayed.
I tried:
ob_start();
echo "Retrieving Data...";
echo str_repeat(' ',1024*64);
sleep(1);
ob_end_clean();
But that didn't work. Is this possible at all?
Output your lines with a . This will return the cursor to 0 on the same line. Where you can write over it.
echo "Retrieving Data..";
sleep(3);
echo "Analyzing Data...";
sleep(3);
echo "Done...
";
I added a to the last echo otherwise the command prompt would over write the last echo.
The following uses the last-of-type pseudo CSS selector to hide all the old progress status messages. It's a reasonably new selector so it doesn't work well on older browsers (pre IE9), you can check the compatibility on the Mozilla Developer Network
<html>
<head>
<style type="text/css">
#progress span {
display: none;
}
#progress span:last-of-type {
display: block; !important
}
</style>
</head>
<body>
<div id="progress">
<?php
for ($i =0; $i<=100; $i+=10) {
ob_start();
echo "<span>$i%</span>";
ob_end_clean();
sleep(1);
}
?>
</div>
</body>
</html>