1

I would like to know is there a way to echo first row from table in database wait/sleep for 5 seconds and then echo the second row?

Thank you in advance for your help!

$conn = mysqli_connect('localhost', 'root', '','database');
$strSql=$conn->query("SELECT words FROM load ORDER BY id ASC ");
if($strSql->num_rows >0) {
    while ($row = $strSql->fetch_assoc()) {
    $rows[]=$row;
    foreach($rows as $row){
       $words1=$row['words'];
               echo '<div class="animatedText"> '.$words1.'</div>';
       //let's say sleep(5); and then print second row???
       }
   }
}
Apb
  • 979
  • 1
  • 8
  • 25
YSimeonov
  • 13
  • 3

2 Answers2

3

try using output buffering. For example:

header( 'Content-type: text/html; charset=utf-8' );

while ($row = $strSql->fetch_assoc()) {
      $rows[]=$row;
       foreach($rows as $row){
            $words1=$row['words'];
            echo '<div class="animatedText"> ';
            echo $words1;
            echo '</div>';
            flush();
            ob_flush();
            sleep(5);
           }
       }
Community
  • 1
  • 1
Gogol
  • 3,033
  • 4
  • 28
  • 57
1

you can use sleep() function:

echo '<div class="animatedText"> ';
while ($row = $strSql->fetch_assoc()) {

      $rows[]=$row;

       foreach($rows as $row){
           $words1=$row['words'];

           echo $words1;
           sleep(5);

           }

       }
echo '</div>';

read more

Gouda Elalfy
  • 6,888
  • 1
  • 26
  • 38
  • I have tried that, but it waits 5 sec and then echo first row...i need something like refresh and echo second row? maybe i don't explaining very well:) sorry – YSimeonov Mar 09 '16 at 11:50
  • to echo the second row after 5 sec. in the same div. i need to display first row in a div, wait 5 sec and display second row in the same div – YSimeonov Mar 09 '16 at 11:59
  • @YSimeonov in this case you print div tag outside the loop once. – Gouda Elalfy Mar 09 '16 at 12:03
  • noc2spam ツ yes actually it is not working the way i want. I'm trying ob_flush() your suggestion :) thanks – YSimeonov Mar 09 '16 at 12:34