Mr.SH Mr.SH - 1 year ago 119
PHP Question

Codeigniter view show progress

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.

my code as bellow

foreach($record->List as $Item)
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.

what i want is:

  1. Show view (empty).

  2. Then keep on printing each line as loop go.


Answer Source

Here you got a example for progress bar using AJAX:


// Start the session.
<!DOCTYPE html>
  <title>Progress Bar</title>
  <script src=""></script>
    #progress {
      width: 500px;
      border: 1px solid #aaa;
      height: 20px;
    #progress .bar {
      background-color: #ccc;
      height: 20px;
  <div id="progress"></div>
  <div id="message"></div>
    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.
        url: "checker.php?file=<?php echo session_id() ?>",
          $("#progress").html('<div class="bar" style="width:' + data.percent + '%"></div>');
          // If the process is completed, we should stop the checking process.
          if (data.percent == 100) {
            timer = window.setInterval(completed, 1000);

    function completed() {

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



// 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) {
} else {
    echo json_encode(array("percent" => null, "message" => null));



// Start the session.

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

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