0

I m trying to iterate the foreach loop using array and scheduling it through sleep function.

below is my code:

<?php
$result = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
$get_count = 4;
$delay = 2;
foreach ($result as $row)
{
echo $row."<br>";
$countx++;
if(($countx % $get_count)==0)
{
sleep($delay);
}
}
?>

The output I'm getting is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

and the output I want is to be show in batch and hide the previos batch :

1
2
3
4

Hide above result and show nxt batch

5
6
7
8

Hide above result and show nxt batch

9
10
11
12

Hide above result and show nxt batch

13
14
15
16

Any help?

1 Answers1

0

Try user client side script as like bellow or use ajax calls to get php array.

<?php
  $result = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16); 
?>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <title>Sleep</title>  
</head>
<body>
<div id="sleep" data-key="0"></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(function() {
    var sleeparray = <?='['.implode(',',$result).']'?>;
    var getcount = 4;
    var disp = $('#sleep'); 
    display(); 

    var IntID = setInterval(function(){    
      if(disp.data('key') >= sleeparray.length)
        clearInterval(IntID);      
      display();    
    },2000)

    function display(){
        var start = disp.data('key');
        var html = '';      

        if(start<sleeparray.length)
        { 
          for(var i=start; i< start+getcount && typeof sleeparray[i] !== 'undefined'; i++){
            html += sleeparray[i]+'<br/>';
          }
          disp.html(html);
          disp.data('key', start+getcount);          
        }        
    }

});
</script>
</body>
</html>
Mohammedshafeek C S
  • 1,916
  • 2
  • 16
  • 26
  • but what to do if i want to display the result in foreach loop – Suman Acoustics Mar 08 '17 at 12:04
  • as per comments on ur questions..for changes in display in a particular interval is only possible by using client side script..not through php script.Foreach u did in php is same converted into javascript array by using implode php function.check the code. – Mohammedshafeek C S Mar 08 '17 at 12:39