Page 1 of 1

How to delay data printing to screen from query within loop

Posted: Sun May 03, 2009 6:36 pm
by monsterpot
Hello all - hope you can help me out here...

I want to be able to delay the output to screen of rows of data from a database. Here's a snippet. i use mysql_fetch_object(). That may/may not be the best way to ge the data and then display. What I want is to display it to screen one line at a time - with a delay of say 2 seconds at a time. Tried sleep() but didn't work. Any ideas?

Thanks

Code: Select all

 
for ($y = 0; $y < $rows; $y++) {
 
$data = @mysql_fetch_object($navresult);
 
echo"<tr align=\"left\" ><td width = \"46%\" align =\"left\" style =\"font-size:12px\">$data->name</td>";
echo"<td width = \"34%\" align =\"left\" style =\"font-size:12px\">$data->number</td>";
echo"<td width = \"10%\" align =\"left\" style =\"font-size:12px\">$data->category</td>";
echo"<td width=\"10%\" ><A title=\"Click here to edit this record\"  onmouseover=\"window.status='click here to edit this record';return true;\"  onclick=\"return true;\" onmouseout=\"window.status='';return true;\"href=\"edit.php?record=$data->record_id\"\"><span style=\"color:#50a7f0\">edit</span></A></td></tr>";
}
echo"</table><BR><BR>";
 

Re: How to delay data printing to screen from query within loop

Posted: Sun May 03, 2009 9:09 pm
by califdon
Remember, PHP is server-side script--it is meaningless to use delays in a PHP script, it would only slow the delivery of the page to the browser. If you want to control the speed at which lines are displayed, you should have your PHP write the lines to an array variable, then in your HTML, include a Javascript routine to display the array elements as you wish.

Re: How to delay data printing to screen from query within loop

Posted: Mon May 04, 2009 3:49 am
by Yossarian
A trick I learned after a while coding PHP is that I can delay echoing output.

Just because your PHP script gets things in a particular order does not mean you need to send them for display in the browser in that same order.

It is very simple to achieve this by continuously adding the output to a variable.

Here is a little example - lets say you select names from an address book application and you want to display them in a HTML table.

(I have left out the database connectivity stuff)

Code: Select all

 
<?
// start the variable
$table = '<table>'  . PHP_EOL ;
 
// loop thru database result set - as you do
  foreach( $rows as $row ){
 
    // here you concatenate strings to the variable $table
     $table .= '<tr><td>' . $row['name'] .  '</td></tr>' . PHP_EOL
 
  }
 
// finally finish off the table
$table . = '</table>' . PHP_EOL
?>
 
Now you get on and do the other stuff you normally do, then at the appropriate moment you do something like this:

Code: Select all

 
<?
 
include $header ;
echo $table ;
include $footer ;
?>
 
Theres a few things about this example that I will explain:

When you concatenate a string you could do this - but it is long winded:

Code: Select all

 
$table = $table . '<tr><td>' . $row['name'] . '</td></tr>' . PHP_EOL ;
 
And the shortcut is to use .=
[gotcha1] Note there is no space between the . and the =

Code: Select all

 
$table .= '<tr><td>' . $row['name'] . '</td></tr>' . PHP_EOL ;
 
[gotcha2] Watch out for concatenating integers, the operator is not the same.

Code: Select all

 
$a= 7;
echo $a .= 6 ; // gives 76 (treats them as 2 strings)
echo $a += 4;  // gives 80
 
The other thing you might be wondering about is the use of the constant PHP_EOL which means "PHP End Of Line", and causes the Operating System to insert it's end of line characters. So your code becomes more portable.

It is the equivalent of echo "\n" ; or echo "\r\n" ; etc. which I always found error_prone and ugly.

This is important if you value easy to read source code for your html as each PHP_EOL will cause a new line to appear.

When sending output to the browser, straight after sending the header info, it is important to send the main thing first (the H1 heading, the main story) but the page's supporting html, like menus and navigation is somewhat secondary.

The order in which you display it on the page may well be very different. Visually the navigation and menus may appear first or most prominently.

Well the same thing is true of your PHP, just because you assemble your navigation elements and breadcrumbs first, does not mean you have to send them to the browser first.

Recognising I could delay output was a small but important step on the road to separating concerns and responsibilities in my code, and in turn helped me evolve from a "PHP/html scripter" to a "PHP programmer".

I hope this helps you.

Re: How to delay data printing to screen from query within loop

Posted: Mon May 04, 2009 4:00 am
by monsterpot
Thanks

I understand I can build the $table variable with values from the database into the eventual complete $table which I could echo as a complete table, but I want to be able to echo one row at a time, with a delay of n seconds between each one. So the table would appear row by row.

Re: How to delay data printing to screen from query within loop

Posted: Mon May 04, 2009 11:31 am
by Yossarian
Take a look at the output buffering functions like flush() and use it with sleep().

If you search google you will likely come across discussions by searching for output buffering incremental display php

Or, send the whole output to the browser and have JS execute what is probably a display effect.