How can I pause in between each iteration in this for loop?

I want to use the "sleep" function in a way where as I'm using a for loop, I'll get a row...then another row...then another row and so on. That, as opposed to the whole collection of rowsin one microsecond.

When I do this, the page will hesitate to load...and then I get all my rows at the same time.

$sql=$conn->prepare("select * from terms");
$sql->execute();
$sum=$sql->rowCount();

$rows= round($sum/10);
if($rows<$sum)
{
	$cycles=$rows+1;
}
else
{
	$cycles=$rows;
}

for($i=1; $i<=$cycles; $i++)
{
	sleep(1);
	echo "How do you do";
	echo "<br>";
}

Open in new window


You can see it at http://brucegust.com/adm/term_class_pdo.php. How can I get it to pause in between each iteration so it rolls out slowly rather than all at once?
Bruce GustPHP DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin MillerCTOCommented:
DO you know about this link:

http://php.net/manual/en/function.sleep.php

Also, you could do a system/operating system call that sleeps.

Note you will be blocked during sleep on your app.

Sorry, my bad re-reading, you're claiming sleep(1), does not work, is that correct?
0
rgranlundCommented:
sleep(3); // this should halt for 3 seconds for every loop
0
Dave BaldwinFixer of ProblemsCommented:
What you are seeing is often a web server problem and not a PHP problem.  Most web servers won't deliver the content until a certain amount of it is ready.  That is entirely separate from the sleep() statement.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I always prefer to do something like this in the browser instead of on the server.

Consider placing your array in javascript, then use js to loop and wait.

<?php
/*
$sql=$conn->prepare("select * from terms");
$sql->execute();
*/

// SIMULATE OUTPUT FROM $SQL TO AN $ARRAY
$array = array(array('title'=>'foo'),array('title'=>'bar'),array('title'=>'baz'));
?>
<script>
var result = <?php echo json_encode($array, JSON_PRETTY_PRINT); ?>
// var result = [ { "a": "foo" }, { "a": "bar" }, { "a": "baz" } ];

result.forEach(function (item, index) {
  setTimeout(function () {
    console.log(item.a);
  }, index * 2000);
});
</script>

Open in new window


Here is a link to the js code http://jsbin.com/jogadinixo/edit?js,console

Where I have console.log, just push the data to a div or li or wherever you need it.
0
Dave BaldwinFixer of ProblemsCommented:
If I use your code above, it takes 17 seconds until it all shows up at once.  If I use the code below, it takes about 5 second before the first instance is displayed and then it takes a little time in between each item.  The file I used is 2.54Kb in size.
<?php 
// Report all PHP errors (see changelog)
error_reporting(E_ALL);

$thetext = file_get_contents("NewQuestion.html");

$cycles = 11;
for($i=1; $i<=$cycles; $i++)
{
	sleep(1);
	echo "Number $i<br>";
	echo $thetext;
	echo "<br>";
}

?>

Open in new window

0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Dave, I don't  know why it is taking you 17 seconds. It should only be 2 seconds per item.

But you can adjust the time to wait by changing the 2000 miliseconds to whatever you want.
index * 2000);

Open in new window


I prefer not using this function server side because it takes up resources and that is why I throw the function to the browser.
0
Dave BaldwinFixer of ProblemsCommented:
Scott, I'm on a slow computer here.  I set it to 11 iterations at '1 second' each and it actually appears to take a little more than that.  The point of the 'demonstration' is that PHP is not the only thing involved.  Web servers that I have used will not return very small amounts of text in a loop like that.  They wait until there is 'enough' or until the process is done.  A 1 second delay may be useful for something that only involves the server code but almost useless when you are displaying small amounts of data thru the web server.
0
Bruce GustPHP DeveloperAuthor Commented:
Guys I took your combined wisdom and I came up with a solution.

Here's what I did:

First page:

$sql=$conn->prepare("select * from terms");
$sql->execute();
$sum=$sql->rowCount();

$rows= round($sum/10);
if($rows<$sum)
{
	$cycles=$rows+1;
}
else
{
	$cycles=$rows;
}

for($i=1; $i<=$cycles; $i++)
{
	echo "How do you do";
	echo "<br>";
}
echo "<script>window.location.href=\"http://brucegust.com/adm/breather.php\";</script>";

Open in new window


This forwards me to the "breather" page where I've got this:

<script>

function backup() {
	window.location.href="forward.php";
}

window.onload = setTimeout(backup, 3000);

</script>

Open in new window


It may not be the most elegant thing in the world, but it does exactly what I was needing as far as running some code...pause...running some and so forth!

Thanks!
1
Bruce GustPHP DeveloperAuthor Commented:
And Martin, just for the sake of honoring your time, I wasn't getting an error but as some of other guys (and this is why I like EE...you're not just getting the "what," you're getting the "why"), so browsers are going to wait until everything is done running before giving you the sleep dynamic like you're wanting it.

So, what I did is forward the whole process to a different page whose sole purpose is to provide a little "wait" gif and then repeat the whole process.

Thanks for weighing in!

rgranlund - you're right but because of the browser dynamic, I wasn't getting the desired affect.
2
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.