在foreach循环php中显示迭代结果批处理,并隐藏先前的批次



i m试图使用数组迭代foreach循环并通过睡眠功能进行安排。

以下是我的代码:

<?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);
}
}
?>

我得到的输出是

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

我想要的输出是在批处理中显示并隐藏previos批次:

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

有帮助吗?

尝试用户客户端脚本,例如bellow或使用AJAX调用来获取PHP数组。

<?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>

最新更新