title: How to: implement a basic php paging with mysql, ajax AND linq
body: <p>Am trying to implement a simple mysql data paging using php, ajax and (the real kicker) linq. There is a linq implementation for php, called phplinq</p>
  
  <p>I have been able to implement the paging without the linq part, and the relevant code is this: (split into several files)</p>
  
  <ol>
  <li><p>pagination.php, where it all starts.</p>
  
  <p> 
      
          
          Pagination with Jquery, Ajax, PHP</p>
  
  <blockquote>
  <pre><code>     &lt;script type="text/javascript"
  </code></pre>
    
    <p>src="js/jquery1_5_2_min.js">
            
            
        
        
            
            
                
                
            
         </p>
  </blockquote></li>
  <li><p>load_data.php, the file where the data is all pulled</p>
  
  <p>
  
  <pre><code>    $query_pag_data = "SELECT id as msg_id,name as message from student LIMIT $start, $per_page";
      $result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
      $msg = "";
      while ($row = mysql_fetch_array($result_pag_data)) {
          $htmlmsg=htmlentities($row['message']);
          $msg .= "&lt;li&gt;&lt;b&gt;" . $row['msg_id'] . "&lt;/b&gt; " . $htmlmsg . "&lt;/li&gt;";
      }
      $msg = "&lt;div class='data'&gt;&lt;ul&gt;" . $msg . "&lt;/ul&gt;&lt;/div&gt;"; // Content for Data
  
      /* --------------------------------------------- */
      $query_pag_num = "SELECT COUNT(*) AS count FROM student";
      $result_pag_num = mysql_query($query_pag_num);
      $row = mysql_fetch_array($result_pag_num);
      $count = $row['count'];
      $no_of_paginations = ceil($count / $per_page);
  
      /* ---------------Calculating the starting and endign values for the loop----------------------------------- */
      if ($cur_page &gt;= 7) {
          $start_loop = $cur_page - 3;
          if ($no_of_paginations &gt; $cur_page + 3)
              $end_loop = $cur_page + 3;
          else if ($cur_page &lt;= $no_of_paginations &amp;&amp; $cur_page &gt; $no_of_paginations - 6) {
              $start_loop = $no_of_paginations - 6;
              $end_loop = $no_of_paginations;
          } else {
              $end_loop = $no_of_paginations;
          }
      } else {
          $start_loop = 1;
          if ($no_of_paginations &gt; 7)
              $end_loop = 7;
          else
              $end_loop = $no_of_paginations;
      }
      /* ----------------------------------------------------------------------------------------------------------- */
      $msg .= "&lt;div class='pagination'&gt;&lt;ul&gt;";
  
      // FOR ENABLING THE FIRST BUTTON
      if ($first_btn &amp;&amp; $cur_page &gt; 1) {
          $msg .= "&lt;li p='1' class='active'&gt;First&lt;/li&gt;";
      } else if ($first_btn) {
          $msg .= "&lt;li p='1' class='inactive'&gt;First&lt;/li&gt;";
      }
  
      // FOR ENABLING THE PREVIOUS BUTTON
      if ($previous_btn &amp;&amp; $cur_page &gt; 1) {
          $pre = $cur_page - 1;
          $msg .= "&lt;li p='$pre' class='active'&gt;Previous&lt;/li&gt;";
      } else if ($previous_btn) {
          $msg .= "&lt;li class='inactive'&gt;Previous&lt;/li&gt;";
      }
      for ($i = $start_loop; $i &lt;= $end_loop; $i++) {
  
          if ($cur_page == $i)
              $msg .= "&lt;li p='$i' style='color:#fff;background-color:#006699;' class='active'&gt;{$i}&lt;/li&gt;";
          else
              $msg .= "&lt;li p='$i' class='active'&gt;{$i}&lt;/li&gt;";
      }
  
      // TO ENABLE THE NEXT BUTTON
      if ($next_btn &amp;&amp; $cur_page &lt; $no_of_paginations) {
          $nex = $cur_page + 1;
          $msg .= "&lt;li p='$nex' class='active'&gt;Next&lt;/li&gt;";
      } else if ($next_btn) {
          $msg .= "&lt;li class='inactive'&gt;Next&lt;/li&gt;";
      }
  
      // TO ENABLE THE END BUTTON
      if ($last_btn &amp;&amp; $cur_page &lt; $no_of_paginations) {
          $msg .= "&lt;li p='$no_of_paginations' class='active'&gt;Last&lt;/li&gt;";
      } else if ($last_btn) {
          $msg .= "&lt;li p='$no_of_paginations' class='inactive'&gt;Last&lt;/li&gt;";
      }
      $goto = "&lt;input type='text' class='goto' size='1' style='margin-top:-1px;margin-left:60px;'/&gt;&lt;input type='button' id='go_btn' class='go_button' value='Go'/&gt;";
      $total_string = "&lt;span class='total' a='$no_of_paginations'&gt;Page &lt;b&gt;" . $cur_page . "&lt;/b&gt; of &lt;b&gt;$no_of_paginations&lt;/b&gt;&lt;/span&gt;";
      $msg = $msg . "&lt;/ul&gt;" . $goto . $total_string . "&lt;/div&gt;";  // Content for pagination
      echo $msg;
  }
  </code></pre></li>
  <li><p>dbconnect.php, containing login stuff</p>
  
  <p></p></li>
  <li><p>paginator.js, the file with javascript functions.</p>
  
  <p>$(document).ready(function(){
      function loading_show(){
          $('#loading').html("").fadeIn('fast');
      }
      function loading_hide(){
          $('#loading').fadeOut('fast');
      }<br>
      function loadData(page){
          loading_show();<br>
          $.ajax
          ({
              type: "POST",
              url: "load_data.php",
              data: "page="+page,
              success: function(msg)
              {
                  $("#container").ajaxComplete(function(event, request, settings)
                  {
                      loading_hide();
                      $("#container").html(msg);
                  });
              }
          });
      }
      loadData(1);  // For first time page load default results
      $('#container .pagination li.active').live('click',function(){
          var page = $(this).attr('p');
          loadData(page);</p>
  
  <pre><code>});           
  $('#go_btn').live('click',function(){
      var page = parseInt($('.goto').val());
      var no_of_pages = parseInt($('.total').attr('a'));
      if(page != 0 &amp;&amp; page &lt;= no_of_pages){
          loadData(page);
      }
      else{
          alert('Enter a PAGE between 1 and '+no_of_pages);
          $('.goto').val("").focus();
          return false;            
      }
  });
  </code></pre>
  
  <p>});</p></li>
  </ol>
  
  <p>And finally, table.css, the file with some basic styling.</p>
  
  <pre><code>body{
      width: 800px;
      margin: 0 auto;
      padding: 0;
  }
  #loading{
      width: 100%;
      position: absolute;
      top: 100px;
      left: 100px;
      margin-top:200px;
  }
  #container .pagination ul li.inactive,
  #container .pagination ul li.inactive:hover{
      background-color:#ededed;
      color:#bababa;
      border:1px solid #bababa;
      cursor: default;
  }
  #container .data ul li{
      list-style: none;
      font-family: verdana;
      margin: 5px 0 5px 0;
      color: #000;
      font-size: 13px;
  }
  
  #container .pagination{
      width: 800px;
      height: 25px;
  }
  #container .pagination ul li{
      list-style: none;
      float: left;
      border: 1px solid #006699;
      padding: 2px 6px 2px 6px;
      margin: 0 3px 0 3px;
      font-family: arial;
      font-size: 14px;
      color: #006699;
      font-weight: bold;
      background-color: #f2f2f2;
  }
  #container .pagination ul li:hover{
      color: #fff;
      background-color: #006699;
      cursor: pointer;
  }
  .go_button
  {
  background-color:#f2f2f2;border:1px solid #006699;color:#cc0000;padding:2px 6px 2px 6px;cursor:pointer;position:absolute;margin-top:-1px;
  }
  .total
  {
  float:right;font-family:arial;color:#999;
  }
  </code></pre>
  
  <p>if youre going to replicate this, there is a css folder and a js folder and i have downloaded the jquery 1.5.2 from google. you can replace the line</p>
  
  <pre><code>&lt;script type="text/javascript" src="js/jquery1_5_2_min.js"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>in pagination.php with</p>
  
  <pre><code>&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>Also, the table being used is </p>
  
  <pre><code>CREATE TABLE student    (
  id INT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(150)
  );
  my
  </code></pre>
  
  <p>objective here is get rid of all direct mysql query calls inside the files with whatever i can put there using linq. I have used linq extensively with .NET 4.0, so basically what am trying to do here is abstracting the data access layer, as if i were using .NET objects to interface with data instead of pure mysql query calls.</p>
  
  <p>Does this make sense? my end goal is being able to replace all mysql query calls with phplinq and completely isolate the data access code.(If linq is an overkill or somehow inadequate, ill drop it)  I dont want to go with any ORM here because am going to heavily customize the grid (In this example there is just a list) and the future Data Access Layer</p>
  
  <p>Am not that proficient   with php, so please dont direct me to extensive reading. something i could pick up and play with would be much better</p>