title: How to implement php-mysql data pagination using data providers
body: <p>Ok. i had created another question about this but i guess i had complicated it a bit.Ill try to simplify my problem here:</p>
  
  <p>Suppose you have this code:</p>
  
  <pre><code>&lt;?php
      if($_POST['page']){
          $page = $_POST['page'];
          $cur_page = $page;
          $page -= 1;
          $per_page = 15;
          $previous_btn = true;
          $next_btn = true;
          $first_btn = true;
          $last_btn = true;
          $start = $page * $per_page;
          include"dbconnect.php";
  
          $query_pag_data = "SELECT msg_id,message from test 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>
  
  <p>that code is part of a larger pagination solution, but my problem is this:</p>
  
  <p>In the code above, am querying data and implementing pagination algorithms on the same place;</p>
  
  <p>This is the approach ive been seeing in every paging tutorial/demo/walk out there.</p>
  
  <p>In my situation, i already have the data i need to paginate in a json format, so i will not be querying the database. basically, how can implement pagination over arrays, for example? looking at the samples out there, they all use LIMIT to paginate, and my mind cant conceive any pagination method without it, but i cannot requery the db.</p>
  
  <p>Has anyone tried to paginate something other that mysql resultsets?</p>