2017-03-08 91 views
0

我嘗試迭代使用數組的foreach循環並通過睡眠函數進行調度。批處理顯示迭代結果並隱藏foreach循環php

下面

是我的代碼:

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

任何幫助嗎?

+0

的可能的複製[什麼是客戶端和服務器端編程的區別?(http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-服務器端編程) –

+0

@u_mulder其不重複 –

+0

PHP無法在客戶端首次顯示之後隱藏任何內容。使用JS的那 –

回答

0

嘗試用戶客戶端腳本像下面的樣子或使用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> 
+0

不工作我試圖 –

+0

開放新的PHP頁面,並把整個代碼並運行它 –

+0

但要怎麼做,如果我想結果顯示在foreach循環 –