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 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化