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条)

报告相同问题?

悬赏问题

  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来