title: how to display real time record count of PHPexcel using Ajax?
body: <p>I want to display real-time record count at front end using <code>PHPexcel</code> and <code>Ajax</code>. I am doing this by creating JSON file and storing data into this. Can I have a better option to show record count in the HTML? It is taking so much time AS the JSON file opening and closing so many times. At the same time, I am requesting this JSON and displaying data on the frontend. I am having this code and sharing you. Please let me know can I have a better option for the same.</p>
  
  <p><strong>File Name: Index.html</strong></p>
  
  <pre><code>&lt;div class="container"&gt;
          &lt;div class="row"&gt;
              &lt;h3&gt;jQuery Ajax file upload with percentage progress bar&lt;/h3&gt;
              &lt;form id="myform" method="post"&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label&gt;Select file: &lt;/label&gt;
                      &lt;input class="form-control" type="file" id="myfile" /&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;!--&lt;div class="progress"&gt;
                          &lt;div class="progress-bar progress-bar-success myprogress" role="progressbar" style="width:0%"&gt;0%&lt;/div&gt;
                      &lt;/div&gt;--&gt;
  
                      &lt;div class="msg"&gt;&lt;/div&gt;
                      &lt;div class="progress"&gt;
                          &lt;div class="progress-bar progress-bar-success progress-bar-striped active" id="progressbar" role="progressbar" style="width:0%"&gt;0%&lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;h2 id="records"&gt;&lt;/h2&gt;
                      &lt;h2 id="consist"&gt;&lt;/h2&gt;
                      &lt;h2 id="hsn"&gt;&lt;/h2&gt;
  
                  &lt;/div&gt;
  
                  &lt;input type="button" id="btn" class="btn-success" value="Upload" /&gt;
              &lt;/form&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
  &lt;script&gt;
        $(function () {
              $('#btn').click(function () {
                  $('.myprogress').css('width', '0');
                  $('.msg').text('');
                  var filename = $('#filename').val();
                  var myfile = $('#myfile').val();
                  if (filename == '' || myfile == '') {
                      alert('Please enter file name and select file');
                      return;
                  }
                  var formData = new FormData();
                  formData.append('myfile', $('#myfile')[0].files[0]);
                  formData.append('filename', filename);
                  $('#btn').attr('disabled', 'disabled');
                 // $('.msg').text('Uploading in progress...');
                  $.ajax({
                      url: 'uploadscript.php',
                      data: formData,
                      processData: false,
                      contentType: false,
                      type: 'POST',
                      /*
                      xhr: function () {
                          var xhr = new window.XMLHttpRequest();  
                          xhr.upload.addEventListener("progress", function (evt) {
                              if (evt.lengthComputable) {
                                  var percentComplete = evt.loaded / evt.total;
                                  percentComplete = parseInt(percentComplete * 100);
                                  $('.myprogress').text(percentComplete + '%');
                                  $('.myprogress').css('width', percentComplete + '%');
                              }
                          }, false);
                          return xhr;
                      },*/
                      success: function (data) {
                          console.log(data);
                          var myData = JSON.parse(data);
                          console.log(myData.records.length);
                          $('#records').text("Records Found : " + myData.records.length).show();
                          $('#consist').text("Consistency : " + (myData.records.length - myData.notConsist.length)).show();
                          //$('.msg').text(data);
                          $('.msg').html(`&lt;div class="alert alert-success"&gt;&lt;strong&gt;Success!&lt;/strong&gt; Upload successfully!&lt;/div&gt;`);
                          $('#btn').removeAttr('disabled');
                          $('.progress').hide();
                      }
                  });   
                  t = setTimeout("updateStatus()", 3000);
              });
          });
  
          function updateStatus(){ 
            $.getJSON('upload/my.json', function(data){      
                                 var items = []; 
  
                                 pbvalue = 0; 
                                 var notExistHSN = 0;
                                 var consistency = 0;
                                 var notConsist = 0;
                                 if(data){ 
  
                                      var total = data['total']; 
  
                                      var current = data['current'];  
  
  
  
  
                                      console.log(current);
  
                                      var pbvalue = Math.floor((current / total) * 100);  
  
                                      if(pbvalue&gt;0){   
  
                                      $('#progressbar').text(pbvalue + '%');
                                      $('#progressbar').css('width', pbvalue + '%');
                                          //console.log(pbvalue);  
                                      }  
                                      $('#records').text("Records Found : " + current).show();
  
  
                                  }  
                                  if(pbvalue &lt; 100){  
  
                                     t = setTimeout("updateStatus()", 3000);  
  
                                  }  
            }).fail(function() {t = setTimeout("updateStatus()", 3000)});  
          } 
      &lt;/script&gt;
  </code></pre>
  
  <p><strong>File Name : uploadscript.php</strong></p>
  
  <pre><code>&lt;?php 
  error_reporting(E_ERROR | E_PARSE);
  $target_dir = 'upload/';
  $target_file = $target_dir . basename($_FILES["myfile"]["name"]);
  
  
  $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
  
  move_uploaded_file($_FILES["myfile"]["tmp_name"], $target_file);
  
  require('Classes/PHPExcel.php');
  require_once "Classes/PHPExcel/IOFactory.php";
  $path = $target_file;
  $fileObj = PHPExcel_IOFactory::load( $path );
  $sheetObj = $fileObj-&gt;getActiveSheet();
  
  
  $startFrom = 1; //default value is 1
  $limit =null;
  $header=array();
  foreach( $sheetObj-&gt;getRowIterator($startFrom, $limit) as $row ){
  
  foreach( $row-&gt;getCellIterator() as $cell ){
      $value = $cell-&gt;getCalculatedValue(); 
      array_push($header,$value);
  }
  break;
  }
  
  $highestRow = $sheetObj-&gt;getHighestRow();
  
  $arr = array('total'=&gt;($highestRow-1), 'current'=&gt;'0', 'notConsist'=&gt;'0', 
  'hsn'=&gt;'0');
  
  file_put_contents("upload/my.json",json_encode($arr));
  
  $startFrom = 2; 
  $limit =count($header);
  $i=1;
  $outp = "";
  foreach( $sheetObj-&gt;getRowIterator($startFrom, $limit) as $row ){
  if ($outp != "") {$outp .= ",";}
  foreach( $row-&gt;getCellIterator() as $key=&gt;$cell){
      // get all total count
      $arr['current'] = "$i"; 
      file_put_contents("upload/my.json",json_encode($arr));
      $value = $cell-&gt;getCalculatedValue();
      if($key==0){$outp .= '{"'.$header[$key].'":"'. $value. '",';}
      else
      if($key==($limit-1)){$outp .= '"'.$header[$key].'":"'. $value. '"}';
  
      }
      else {        
      $outp .= '"'.$header[$key].'":"'. $value. '",';
      }
    }
    $i++;
   }
   $outp ='{"records":['.$outp.']}';
   echo $outp;
  </code></pre>
  
  <p>?></p>