title: Page numeration without LIMIT statement and with sorting
body: <p>I have an article system which is used by several users. Articles have their own permissions which gets checked against the viewer's ID for each article that runs through my while loop. I limit the articles by incrementing <code>$printcount</code> if the user is allowed to view the article, and the loop terminates when it reaches <code>$limit</code>. I also have sorting options to sort by name, date inserted, date modified and submitted by.</p>
  
  <p>How would I go about utilizing next/previous page buttons without using MySQL limit features? Here is a short version of my code (may not be 100% functional, but should bring the point across)</p>
  
  <pre><code>&lt;?php
  $sort=$_GET['sort']; // date_added
  $direction=$_GET['dir']; // ASC
  $limit=$_GET['limit']; // 25
  $query="SELECT * FROM `article` ORDER BY '$sort' '$direction'";
  
  $result=mysql_query($query);
  if(mysql_num_rows($result)==NULL) {
    echo '&lt;tr&gt;&lt;td colspan="4"&gt;NO RECORDS FOUND&lt;/td&gt;&lt;/tr&gt;';
  }
  $printcount=0;
  while(($row=mysql_fetch_assoc($result))&amp;&amp;($printcount&lt;$limit+1)) {
    //if user is owner of article $readable=true;
    //load permissions from permission column (comma separated integers) into array, if user's group ID is in that array $readable=true;
    //if article "global_read" flag=1, $readable=true;
    //if $printcount&lt;$limit, and $readable echo the table row with data
    //if $readable, $printcount++;
  }
  ?&gt;
  </code></pre>
  
  <p>If I were only sorting by <code>ID ASC</code>, I could easily store the ID as a <code>last_id</code>, and on next page the query would have an additional: <code>WHERE <code>`id`</code> > $last_ID</code>, but since there are different sort options and direction options this seems to be much more complicated.</p>