title: Using session variable for load more
body: <p>My issue is am using jQuery to auto refresh the div but am using the following script to keep the record track, so if the user clicks on Load More I load the posts, but the moment jQuery refreshes the div, it resets back to the default, any idea how do I preserve the number of loaded record</p>
  
  <p>This is what I tried</p>
  
  <pre><code>if(isset($_POST['load_more'])) {
      $_SESSION['total_counter'] = $_SESSION['total_counter'] + 10;
      $fetch_messages = mysqli_query($connection, "SELECT tbl_chat.record_date, msg_from, message, user_pic, tbl_chat.id, tbl_chat.record_date, real_name FROM tbl_chat JOIN tbl_users ON msg_from = tbl_users.id ORDER BY id DESC LIMIT ".$_SESSION['total_counter']);
  } else {
      $counter = 10;
      $_SESSION['total_counter'] = $counter;
      $fetch_messages = mysqli_query($connection, "SELECT tbl_chat.record_date, msg_from, message, user_pic, tbl_chat.id, tbl_chat.record_date, real_name FROM tbl_chat JOIN tbl_users ON msg_from = tbl_users.id ORDER BY id DESC LIMIT ".$_SESSION['total_counter']);
  }
  </code></pre>
  
  <p>What happens is on refresh it uses the <code>$counter</code> value and forgets the load more session value</p>
  
  <p>Detailed Code</p>
  
  <pre><code>&lt;?php
      require_once 'connection.php';
      require_once 'functions.php';
      if(!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] == false) {
          header('Location: login.php');
          exit;
      }
      date_default_timezone_set("Asia/Calcutta");
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"&gt;&lt;/script&gt;
          &lt;script type="text/javascript"&gt;
              jQuery(document).ready(function(){
                  jQuery(".toggle_container").show();
                  jQuery("div.question_trigger").click(function(){
                  jQuery(this).toggleClass("active").next().toggle("slow");
                  });
              });
          &lt;/script&gt;
          &lt;script type="text/javascript"&gt;
              var auto_refresh = setInterval(function() {
                    $('.messages').load('chat.php .messages', function(){
                         $(this).fadeIn("fast")
                    });
              }, 2000);
          &lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;?php
              require_once 'header.php';
              if($_SESSION['user_id'] == 1) {
                  $clear_notifications = mysqli_query($connection, "UPDATE tbl_chat_notifications SET checked_first = 'yes' WHERE checked_first = ''");
              } elseif ($_SESSION['user_id'] == 2) {
                  $clear_notifications = mysqli_query($connection, "UPDATE tbl_chat_notifications SET checked_second = 'yes' WHERE checked_second = ''");
              } elseif ($_SESSION['user_id'] == 3) {
                  $clear_notifications = mysqli_query($connection, "UPDATE tbl_chat_notifications SET  checked_third = 'yes' WHERE checked_third = ''");
              }   
          ?&gt;
          &lt;div class="container"&gt;
              &lt;form method="POST"&gt;
                  &lt;textarea name="messaging"&gt;&lt;/textarea&gt;
                  &lt;input type="submit" value="Post" name="message_input" class="gen_button" /&gt;
              &lt;/form&gt;
              &lt;img src="smileys/smile.gif" /&gt;&lt;a target="_blank" href="smileys_info.php"&gt; Smileys Info&lt;/a&gt;
              &lt;br /&gt;&lt;br /&gt;
              &lt;?php
              if(isset($_POST['message_input'])) {
                  $throw_error = array();
                  if(isset($_POST['messaging']) &amp;&amp; $_POST['messaging'] == '') {
                      $throw_error['msg_blank'] = "Message Cannot Be Blank";
                  }
  
                  if(empty($throw_error)) {
                      $message = nl2br(htmlspecialchars(mysqli_real_escape_string($connection, $_POST['messaging'])));
                      $date_time = date("Y-m-d H:i:s");
                      if(!mysqli_query($connection, "INSERT INTO tbl_chat (msg_from, message, record_date) VALUES('".$_SESSION['user_id']."', '$message', '$date_time')")) {
                          echo mysqli_error($connection);
                      }
  
                      if($_SESSION['user_id'] == 1) {
                          if (!mysqli_query($connection, "INSERT INTO tbl_chat_notifications (notification_of, checked_first) VALUES('".$_SESSION['user_id']."', 'yes')")) {
                              echo mysqli_error($connection);
                          } 
                      } elseif ($_SESSION['user_id'] == 2) {
                          if (!mysqli_query($connection, "INSERT INTO tbl_chat_notifications (notification_of, checked_second) VALUES('".$_SESSION['user_id']."', 'yes')")) {
                              echo mysqli_error($connection);
                          }
                      } elseif ($_SESSION['user_id'] == 3) {
                          if (!mysqli_query($connection, "INSERT INTO tbl_chat_notifications (notification_of, checked_third) VALUES('".$_SESSION['user_id']."', 'yes')")) {
                              echo mysqli_error($connection);
                          }
                      }
                      else {
                          header('Location: chat.php');
                          exit;
                      }
                  }
  
                  if(!empty($throw_error)) {
                      if(isset($throw_error['msg_blank'])) {
                          echo "&lt;span class=\"red\"&gt;".$throw_error['msg_blank']."&lt;/span&gt;";
                      }
                  }
              }
  
              if(isset($_POST['load_more'])) {
                  $_SESSION['total_counter'] = $_SESSION['total_counter'] + 10;
                  $fetch_messages = mysqli_query($connection, "SELECT tbl_chat.record_date, msg_from, message, user_pic, tbl_chat.id, tbl_chat.record_date, real_name FROM tbl_chat JOIN tbl_users ON msg_from = tbl_users.id ORDER BY id DESC LIMIT ".$_SESSION['total_counter']);
  
              } else {
                  $counter = 10;
                  $_SESSION['total_counter'] = $counter;
                  $fetch_messages = mysqli_query($connection, "SELECT tbl_chat.record_date, msg_from, message, user_pic, tbl_chat.id, tbl_chat.record_date, real_name FROM tbl_chat JOIN tbl_users ON msg_from = tbl_users.id ORDER BY id DESC LIMIT ".$_SESSION['total_counter']);
              }
              ?&gt;
              &lt;div class="messages"&gt;
              &lt;?php
                  while($throw_messages = mysqli_fetch_array($fetch_messages)) {
              ?&gt;
              &lt;div class="message_container"&gt;
                  &lt;div class="display_pic"&gt;
                      &lt;img src="&lt;?php echo $throw_messages['user_pic']; ?&gt;" /&gt;
                  &lt;/div&gt;
                  &lt;div class="user_name"&gt;
                      &lt;?php
                          echo $throw_messages['real_name'];
                      ?&gt;
                  &lt;/div&gt;
                  &lt;div class="message"&gt;&lt;?php smilies($throw_messages['message']); ?&gt;&lt;/div&gt;
                  &lt;div class="msg_date_time"&gt;&lt;?php echo explode_date_time($throw_messages['record_date']); ?&gt;&lt;/div&gt;
              &lt;/div&gt;
              &lt;?php } ?&gt;
              &lt;/div&gt;
              &lt;?php
                  if(mysqli_num_rows($fetch_messages) &gt; 9) {
              ?&gt;
              &lt;form method="POST"&gt;
                  &lt;input type="submit" value="Load More" name="load_more" /&gt;
              &lt;/form&gt;
              &lt;?php
                  }
              ?&gt;
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>