drddx3115 2017-12-19 07:55
浏览 43
已采纳

PHP Codeigniter加载页面,具有睡眠延迟

I want to fetch data from database and show the result in codeigniter view with delay.

My code looks like this :

   function loadPage($data) {
       for($i = 0; $i < count($data); $i++) {
          $this->load->view('mypage', $data[$i]);
          sleep(5);
       }
   }

But this only load page once and show all data. What I need is load page with $data[0], delay 5 secs, load page with $data[1], and so on.

Please advice. Thank You.

Edit :

I want to do this with php. Not javascript or jquery.

  • 写回答

2条回答 默认 最新

  • douju3911 2017-12-19 10:15
    关注

    PHP processes in the server and returns the processed html to the browser. That's how it works. But as you have explained in the comments, I think what you wanted to do is display the content to the browser while the for loop is running.

    To do that you need to use output buffering (AFAIK, CI already calls ob_* methods in the core) so it might cause problems (or may not work as you would expect.) However, instead of loading the view multiple times, I suggest you to pass the data to the view and loop it there (and display the result one by one with a sleep.)

    You can do it this way.

    in your controller:

    public function index(){
        $data = [1,2,3,4,5];
        $this->load->view('mypage', ['data' => $data]);
    }
    

    In your view (mypage.php):

    <?php
    while (@ob_end_flush());      
    ob_implicit_flush(true);
    
    for($i = 0; $i < count($data); $i++) {
        echo $data[$i];
        sleep(3);
    }
    ?>
    

    Hope it helps :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?