Mr.SH Mr.SH - 4 months ago 74
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

$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.

what i want is:


  1. Show view (empty).

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



Thanks

Answer

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);
}

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