title: Live progress status reporting using AJAX and PHP
body: <p>Good day everyone, I'm trying to create a live progress status report for my automatic course scheduler. Currently, I tried using session variables to achieve my expected functionality but still no luck. Also, some progress report messages aren't displaying and the session variable doesn't seem to update. I would like to seek for help or advice to the stated problems.</p>
  <p>For further information, I included below my code and expected and actual results. Thank you</p>
  <p><strong>Expected Result for Progress Status Reporting When The System is Automatically Scheduling Courses:</strong><br></p>
  <ul>
  <li>[Automatic Course Scheduler]: Initiating to schedule courses that have conflict...</li>
  <li>[Automatic Course Scheduler]: All courses are successfully scheduled</li>
  <li>[Automatic Course Scheduler]: Initiating to schedule courses that have no conflict...</li>
  <li>[Automatic Course Scheduler]: All courses that have no conflict are now all scheduled.</li>
  </ul>
  <p><strong>Actual Result for Progress Status Reporting When The System is Automatically Scheduling Courses</strong> <br>
  <a href="https://i.stack.imgur.com/gwoQC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gwoQC.png" alt="enter image description here" /></a></p>
  <p><strong><code>CODE</code>:</strong></p>
  <p><code>Javascript</code> (<code>JQuery</code> Lib)</p>
  <pre><code>$('.left-section').on('click', '#sp-auto-courses', function(){
  
              
              $.ajax({
                  url: 'application_layer/automated_course_scheduler.php',
                  type: 'post',
                  success: function(response,status){
  
                  }  
  
              });
              getProgress();
              function getProgress(){
  
                  $.ajax({
                  url: 'application_layer/schedulingLogRealTime.php',
                  type: 'post',
                  success: function(response,status){
                      if(response.length &gt; 0){
                              
                                  $('.schedulinglogs').append(response);
                                  getProgress();
                          }else{
                                  $(&quot;#sw-select-date&quot;).val(&quot;0&quot;).change();
                                      $('#table-responsive').empty();
                                      $('#table-responsive').load('application_layer/course-pagination.php');
                          }
  
                                  
  
                      }  
  
                  });
  
  
  
              }
          
      
  
  
      });
  </code></pre>
  <p>PHP</p>
  <p><strong><code>schedulingLogRealTime.php</code></strong></p>
  <pre><code>&lt;?php
  session_start();
  
  if (isset($_SESSION[&quot;acs_progress&quot;]) &amp;&amp; strlen($_SESSION[&quot;acs_progress&quot;]) &gt; 0) {
      sleep(1);
      echo $_SESSION[&quot;acs_progress&quot;];
      unset($_SESSION[&quot;acs_progress&quot;]);
  
  
  }
  ?&gt;
  </code></pre>
  <p><strong><code>automated_course_scheduler.php</code></strong></p>
  <pre><code>&lt;?php 
  
  require &quot;dbcaller.php&quot;;
  
  $timeslotpos = 1;
  $inValues = array();
  $scheduledTimeslot = array();
  //$commaSeparatedInValues = &quot;&quot;;
  $coursePrioritization = false;
  $processResult = array();
  $selectWhereNoConflict = array();
  
  $messageLog = array();
  //SPREAD CONFLICT CONSTRAINTS DETECT
  $spreadConflict = &quot;true&quot;;
  $selectAllCourseThatHaveConflict = $db-&gt;query(&quot;Select DISTINCT courses.Course_ID from courses INNER JOIN course_conflict ON course_conflict.Course_ID = courses.Course_ID WHERE CDataset_ID = 1 AND NOT EXISTS (SELECT Course_ID from examination_schedule WHERE course_conflict.Course_ID = examination_schedule.Course_ID)&quot;);
  
  //CHECK If There Are Room Scheduled
  $queryIfThereAreScheduledRooms = $db-&gt;query( &quot;Select RoomID from examination_schedule Where ExamEvent_ID=1 AND RoomID !=0&quot;);
  
  if($queryIfThereAreScheduledRooms-&gt;numRows() == 0){
  
      if($selectAllCourseThatHaveConflict-&gt;numRows() &gt; 0){
          $coursesThatHaveConflict = $selectAllCourseThatHaveConflict-&gt;fetchAll();    
           storeMessageToSession(&quot;&lt;div&gt;&lt;p style='display: inline'&gt;[Auto Course Scheduler]: &lt;p style='color: #8B8000;display: inline'&gt;Initiating to schedule courses that have conflict... &lt;/p&gt;&lt;/p&gt;&lt;/div&gt;&quot;);
               
           
                  foreach($coursesThatHaveConflict as $row){
  
                      //CHECK IF THERE ARE SCHEDULED
                      $get_dataIfThereAreScheduled = $db-&gt;query(&quot;Select TimeslotPos, ExamScheduleID, Timeslot_ID from examination_schedule WHERE ExamEvent_ID=1 AND Course_ID=0&quot;)-&gt;fetchArray();
                      foreach($get_dataIfThereAreScheduled as $data){
  
                              $scheduledTimeslot[] = $data;   
                      }
  
                              
                      //NEED TO KNOW THE COURSE CONFLICT OF THE COURSES
                      $queryConflict = $db-&gt;query(&quot;SELECT course_conflict.ConflictCourse FROM course_conflict INNER JOIN courses ON course_conflict.Course_ID = courses.Course_ID WHERE courses.CDataset_ID = 1 AND course_conflict.Course_ID= ?&quot;, $row['Course_ID'])-&gt;fetchAll();
  
                      foreach($queryConflict as $row2) {
  
                              $inValues[] = $row2['ConflictCourse'];
                          
                  
                      }
              
                      if($spreadConflict == &quot;false&quot;){
                           $selectWhereNoConflict = $db-&gt;query(&quot;Select DISTINCT timeslot.Timeslot_ID from timeslot WHERE ExamEvent_ID=1 AND NOT EXISTS (SELECT Timeslot_ID from examination_schedule WHERE timeslot.Timeslot_ID = examination_schedule.Timeslot_ID AND examination_schedule.Course_ID IN (&quot;.rtrim(str_repeat(&quot;?,&quot;, count($inValues)), &quot;,&quot;).&quot;) AND examination_schedule.ExamEvent_ID=1) ORDER BY STR_TO_DATE (timeslot.Date, '%M %e, %Y'),  STR_TO_DATE (CONCAT(timeslot.Start_Time,' - ',timeslot.End_Time), '%l %p - %l %p') LIMIT 1;&quot;, $inValues);
  
                      }else if($spreadConflict==&quot;true&quot;){
  
  
                           $selectWhereNoConflict = $db-&gt;query(&quot;Select DISTINCT timeslot.Timeslot_ID from timeslot WHERE ExamEvent_ID=1 AND NOT EXISTS (SELECT Timeslot_ID from examination_schedule WHERE timeslot.Timeslot_ID = examination_schedule.Timeslot_ID AND examination_schedule.Course_ID IN (&quot;.rtrim(str_repeat(&quot;?,&quot;, count($inValues)), &quot;,&quot;).&quot;) AND examination_schedule.ExamEvent_ID=1) AND NOT EXISTS (Select examination_schedule.Timeslot_ID from examination_schedule WHERE timeslot.Timeslot_ID = examination_schedule.Timeslot_ID AND examination_schedule.ExamEvent_ID=1) ORDER BY STR_TO_DATE (CONCAT(timeslot.Start_Time,' - ',timeslot.End_Time), '%l %p - %l %p') LIMIT 1;&quot;,  $inValues);   
  
                          if($selectWhereNoConflict-&gt;numRows() == 0){
                              $selectWhereNoConflict = $db-&gt;query(&quot;Select DISTINCT timeslot.Timeslot_ID from timeslot WHERE ExamEvent_ID=1 AND NOT EXISTS (SELECT Timeslot_ID from examination_schedule WHERE timeslot.Timeslot_ID = examination_schedule.Timeslot_ID AND examination_schedule.Course_ID IN (&quot;.rtrim(str_repeat(&quot;?,&quot;, count($inValues)), &quot;,&quot;).&quot;) AND examination_schedule.ExamEvent_ID=1) ORDER BY STR_TO_DATE (CONCAT(timeslot.Start_Time,' - ',timeslot.End_Time), '%l %p - %l %p') LIMIT 1;&quot;, $inValues);  
  
                              storeMessageToSession(&quot;&lt;div&gt;&lt;p style='display: inline'&gt;[Auto Course Scheduler]:  &lt;p style='color: red;display: inline'&gt;Not Enough Timeslot To Equally Spread Each Course: Finding Best Timeslot for &quot;.$row['Course_ID'].&quot;&lt;/p&gt;&lt;/p&gt;&lt;/div&gt;&quot;);      
                               
  
                      
  
                          }   
                      
  
                      }
  
                       if($selectWhereNoConflict-&gt;numRows() &gt; 0){
                              $newTimeslotPos = 0;
                              $timeslotId = $selectWhereNoConflict-&gt;fetchArray();
                          
                              if(in_array($timeslotId['Timeslot_ID'], array_column($scheduledTimeslot, 'Timeslot_ID')) AND !empty($scheduledTimeslot)){
                                  
                                      foreach($scheduledTimeslot as $data){
  
                                          $updateRowWithCourse = $db-&gt;query(&quot;UPDATE `examination_schedule` SET `Course_ID`= ? WHERE ExamScheduleID= ?&quot;, $row['Course_ID'], $data['ExamScheduleID']);
                                          break;
                                      
                                      }
                                      //$commaSeparatedInValues = &quot;&quot;;         
                              }else{
  
                          
                                  $selectCurrentTimeslotPos = $db-&gt;query(&quot;SELECT COALESCE(MAX(TimeslotPos), 0) as CurrentTimeslotPos FROM examination_schedule WHERE ExamEvent_ID=1 AND Timeslot_ID= ?&quot;, $timeslotId['Timeslot_ID']);
  
                                  if($selectCurrentTimeslotPos-&gt;numRows() &gt; 0){
                                      $currTimeslotPos = $selectCurrentTimeslotPos-&gt;fetchArray();
                                      $newTimeslotPos = $currTimeslotPos['CurrentTimeslotPos'] + 1;
                                  }else{
                                      $newTimeslotPos = 1;
                                  }
  
                                  $insertCourse = $db-&gt;query(&quot;INSERT INTO `examination_schedule`(`Course_ID`, `Timeslot_ID`, `ExamEvent_ID`, `TimeslotPos`) VALUES (?,?,'1',?)&quot;, $row['Course_ID'], $timeslotId['Timeslot_ID'], $newTimeslotPos);
                                  //      $commaSeparatedInValues = &quot;&quot;;
                              }
                                  
                       }else{
                           storeMessageToSession(&quot;&lt;div&gt;&lt;p style='display: inline'&gt;[Auto Course Scheduler]: &lt;p style='color: red;display: inline'&gt;Cant Schedule Course ID: &quot;.$row['Course_ID'].&quot; due to many conflicts&lt;/p&gt;&lt;/p&gt;&lt;/div&gt;&quot;);
                           
                  
  
                       }  
  
                      $inValues = array();
                      //$commaSeparatedInValues = &quot;&quot;;
                      $coursePrioritization = true;
                      $scheduledTimeslot = array();
                      $selectWhereNoConflict = &quot;&quot;;
  
              }
  
              
                  storeMessageToSession(&quot;&lt;div&gt;&lt;p style='display: inline'&gt;[Auto Course Scheduler]: &lt;p style='color: green;display: inline'&gt;All courses are successfully scheduled&lt;/p&gt;&lt;/p&gt;&lt;/div&gt;&quot;);
                   
      
      }else{
              $coursePrioritization = true;
              
               storeMessageToSession(&quot;&lt;div &gt;&lt;p style='display: inline'&gt;[Auto Course Scheduler]:  &lt;p style='color: green;display: inline'&gt;All courses that have conflict are already scheduled.&lt;/p&gt;&lt;/p&gt;&lt;/div&gt;&quot;);
               
  
  
      }
          
  
      if($coursePrioritization == true){
              
              $selectAllUnscheduledCourse = $db-&gt;query(&quot;Select DISTINCT courses.Course_ID from courses WHERE CDataset_ID = 1 AND NOT EXISTS (SELECT Course_ID from examination_schedule WHERE courses.Course_ID = examination_schedule.Course_ID)&quot;);
  
                  if($selectAllUnscheduledCourse-&gt;numRows() == 0){
  
                       storeMessageToSession(&quot;&lt;div&gt;&lt;p style='display: inline'&gt;[Auto Course Scheduler]: &lt;p style='color: green;display: inline'&gt;All courses that have no conflict are already scheduled.&lt;/p&gt;&lt;/p&gt;&lt;/div&gt;&quot;);
                       
  
  
                  }else{
  
                           storeMessageToSession(&quot;&lt;div&gt;&lt;p style='display: inline'&gt;[Auto Course Scheduler]: &lt;p style='color: #8B8000;display: inline'&gt;Initiating to schedule courses that have no conflict... &lt;/p&gt;&lt;/p&gt;&lt;/div&gt;&quot;);
                           
                          $unscheduledCourses = $selectAllUnscheduledCourse-&gt;fetchAll();
                          foreach($unscheduledCourses as $row){
  
                                  //CHECK IF THERE ARE SCHEDULED (REFRESH)
                                  $get_dataIfThereAreScheduled = $db-&gt;query(&quot;Select TimeslotPos, ExamScheduleID, Timeslot_ID from examination_schedule WHERE ExamEvent_ID=1 AND Course_ID=0&quot;)-&gt;fetchArray();
                                  foreach($get_dataIfThereAreScheduled as $data){
  
                                          $scheduledTimeslot[] = $data;   
                                  }
  
                                  $newTimeslotPos = 0;
                                  $timeslotId = array();
  
  
                                      
                                  $timeslotId = $db-&gt;query(&quot;Select DISTINCT timeslot.Timeslot_ID from timeslot WHERE timeslot.ExamEvent_ID=1 ORDER BY RAND() LIMIT 1;&quot;)-&gt;fetchArray();                    
  
                                  if(in_array($timeslotId['Timeslot_ID'], array_column($scheduledTimeslot, 'Timeslot_ID')) AND !empty($scheduledTimeslot)){
                                              
                                      foreach($scheduledTimeslot as $data){
  
                                          $updateRowWithCourse = $db-&gt;query(&quot;UPDATE `examination_schedule` SET `Course_ID`='&quot;.$row['Course_ID'].&quot;' WHERE ExamScheduleID=?&quot;, $data['ExamScheduleID']);
                                          break;
                                  
                                      }
  
                                                      
                                  }else{
  
                                          $selectCurrentTimeslotPos = $db-&gt;query(&quot;SELECT COALESCE(MAX(TimeslotPos), 0) as CurrentTimeslotPos FROM examination_schedule WHERE ExamEvent_ID=1 AND Timeslot_ID= ?&quot;, $timeslotId['Timeslot_ID']);
                                          if($selectCurrentTimeslotPos-&gt;numRows() &gt; 0){
                                              $currTimeslotPos = $selectCurrentTimeslotPos-&gt;fetchArray();
                                              $newTimeslotPos = $currTimeslotPos['CurrentTimeslotPos'] + 1;
                                          }else{
                                              $newTimeslotPos = 1;
                                          }
  
                                          $insertCourse = $db-&gt;query(&quot;INSERT INTO `examination_schedule`(`Course_ID`, `Timeslot_ID`, `ExamEvent_ID`, `TimeslotPos`) VALUES (?,?,'1',?)&quot;, $row['Course_ID'], $timeslotId['Timeslot_ID'], $newTimeslotPos);
  
  
                                  }
  
                              $scheduledTimeslot = array();
                          }
  
  
      
  
                       storeMessageToSession(&quot;&lt;div&gt;&lt;p style='display: inline'&gt;[Auto Course Scheduler]: &lt;p style='color: green;display: inline'&gt;All courses that have no conflict are now all scheduled. &lt;/p&gt;&lt;/p&gt;&lt;/div&gt;&quot;);
                       
                              
  
                  }
                      
      }
  
  }else{
  
      storeMessageToSession(&quot;&lt;div&gt;&lt;p style='display: inline'&gt;[Auto Course Scheduler]: &lt;p style='color: red;display: inline'&gt;The system can't initiate the automatic course scheduler due to some rooms are scheduled &lt;/p&gt;&lt;/p&gt;&lt;/div&gt;&quot;);
      
  
  
  }
  
  
  function storeMessageToSession($message){
  
  
      session_start();
      $_SESSION[&quot;acs_progress&quot;] = $message;
      session_write_close();
  
  }
  
  ?&gt;
  </code></pre>