Codeigniter视图显示进度 [英] Codeigniter view show progress

查看:51
本文介绍了Codeigniter视图显示进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Codeigniter,并希望显示XML导入的进度.但我面临的问题是当查看加载时,它卡在加载中(空白页),当我可以查看时,它显示100%完成.

I am using Codeigniter and want to show progress of XML import. but issue i am facing is When view load, it stuck on loading (blank page) and when i can see view its shows 100% done.

我的代码如下

$i=0;
$count=sizeof($record->List);
foreach($record->List as $Item)
{
$i++;
echo "processing ".$i." of ".$count;

      ---processing code which takes times to process---
      ---processing code which takes times to process---


}

此代码处于视图中,但是当我单击链接以加载此视图时,我必须等待所有过程完成,然后在所有过程完成后才能看到视图.

this code is in view but when i click on link to load this view, i have to wait for all process to complete and then i can see view when all process is done.

我想要的是:

  1. 显示视图(空).

  1. Show view (empty).

然后继续循环打印每一行.

Then keep on printing each line as loop go.

谢谢

推荐答案

在这里,您将获得一个使用AJAX的进度条示例:

Here you got a example for progress bar using AJAX:

index.php

<?php
// Start the session.
session_start();
?>
<!DOCTYPE html>
<html>
<head>
  <title>Progress Bar</title>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
  <style>
    #progress {
      width: 500px;
      border: 1px solid #aaa;
      height: 20px;
    }
    #progress .bar {
      background-color: #ccc;
      height: 20px;
    }
  </style>
</head>
<body>
  <div id="progress"></div>
  <div id="message"></div>
  <script>
    var timer;

    // The function to refresh the progress bar.
    function refreshProgress() {
      // We use Ajax again to check the progress by calling the checker script.
      // Also pass the session id to read the file because the file which storing the progress is placed in a file per session.
      // If the call was success, display the progress bar.
      $.ajax({
        url: "checker.php?file=<?php echo session_id() ?>",
        success:function(data){
          $("#progress").html('<div class="bar" style="width:' + data.percent + '%"></div>');
          $("#message").html(data.message);
          // If the process is completed, we should stop the checking process.
          if (data.percent == 100) {
            window.clearInterval(timer);
            timer = window.setInterval(completed, 1000);
          }
        }
      });
    }

    function completed() {
      $("#message").html("Completed");
      window.clearInterval(timer);
    }

    // When the document is ready
    $(document).ready(function(){
      // Trigger the process in web server.
      $.ajax({url: "process.php"});
      // Refresh the progress bar every 1 second.
      timer = window.setInterval(refreshProgress, 1000);
    });
  </script>
</body>
</html>

checker.php

<?php

// The file has JSON type.
header('Content-Type: application/json');

// Prepare the file name from the query string.
// Don't use session_start here. Otherwise this file will be only executed after the process.php execution is done.
$file = str_replace(".", "", $_GET['file']);
$file = "tmp/" . $file . ".txt";

// Make sure the file is exist.
if (file_exists($file)) {
    // Get the content and echo it.
    $text = file_get_contents($file);
    echo $text;

    // Convert to JSON to read the status.
    $obj = json_decode($text);
    // If the process is finished, delete the file.
    if ($obj->percent == 100) {
        unlink($file);
    }
} else {
    echo json_encode(array("percent" => null, "message" => null));
}

process.php

<?php

// Start the session.
session_start();

// The example total processes.
$total = 20;

// The array for storing the progress.
$arr_content = array();

// Loop through process
for ($i = 1; $i <= $total; $i++) {
    // Calculate the percentation
    $percent = intval($i / $total * 100);

    // Put the progress percentage and message to array.
    $arr_content['percent'] = $percent;
    $arr_content['message'] = $i . " row(s) processed.";

    // Write the progress into file and serialize the PHP array into JSON format.
    // The file name is the session id.
    file_put_contents("tmp/" . session_id() . ".txt", json_encode($arr_content));

    // Sleep one second so we can see the delay
    sleep(1);
}

只需在CI上编写它并创建一个tmp文件夹,然后在脚本中指向它的路径即可.祝你好运!

Just that you write it on the CI and create a tmp folder, and you point a path to it in script. Good luck!

这篇关于Codeigniter视图显示进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆