title: PHP pagination - problem displaying correct content on additional pages
body: <p>I found this awesome code online to help with pagination, and it's working well, the only problem is: each page displays the same 4 rows.</p>
  
  <p>Any ideas will be much appreciated</p>
  
  <pre><code>&lt;?php
          //Include the PS_Pagination class
          include('includes/ps_pagination.php');
  
          //Connect to mysql db
          $conn = mysql_connect("localhost", "root", "root");
          mysql_select_db('database',$conn);
          $sql = "SELECT * FROM studies WHERE niche = '{$_GET['niche']}'";
          //Create a PS_Pagination object
          $pager = new PS_Pagination($conn,$sql,4,5);
          //The paginate() function returns a mysql
          //result set for the current page
          $rs = $pager-&gt;paginate();
          //Loop through the result set
          while($row = mysql_fetch_assoc($rs)) {
  
      $a=0;
      while ($a &lt; $num) {
  
      $id=mysql_result($result,$a,"id");
      $title=mysql_result($result,$a,"title");
      $strategies=mysql_result($result,$a,"strategies");
      $client=mysql_result($result,$a,"client");
      $copy=mysql_result($result,$a,"copy");
      $thumbmedia=mysql_result($result,$a,"thumbmedia");
      $niche=mysql_result($result,$a,"niche");
  
  
      echo '&lt;div class="container"&gt;&lt;p class="subheadred"&gt;&lt;a href="casestudy.php?id='.$id.'"&gt;'.$title.'&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;';
  
      echo '&lt;div class="containerstudy"&gt;&lt;div class="column1"&gt;&lt;p class="subheadsmall"&gt;Strategies&lt;/p&gt;&lt;p class="sidebarred"&gt;'.$strategies.'&lt;/p&gt;';
  
      echo '&lt;p class="subheadsmall"&gt;Client&lt;/p&gt;&lt;p class="sidebargrey"&gt;'.$client.'&lt;/p&gt;&lt;/div&gt;';
  
      echo '&lt;div class="column2"&gt;&lt;p class="bodygrey"&gt;'.substr($copy, 0, 300).'&lt;a href="casestudy.php?id='.$id.'"&gt;...more&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;';
  
      echo '&lt;div class="column3"&gt;&lt;img src="images/'.$thumbmedia.'" height="160" /&gt;&lt;/div&gt;&lt;/div&gt;';
  
      $a++;
      }
  
          }
          //Display the navigation
          echo $pager-&gt;renderNav();
      ?&gt;
  </code></pre>
  
  <p>This is the included file:</p>
  
  <pre><code>&lt;?php
  /**
   * PHPSense Pagination Class
   *
   * PHP tutorials and scripts
   *
   * @package     PHPSense
   * @author      Jatinder Singh Thind
   * @copyright   Copyright (c) 2006, Jatinder Singh Thind
   * @link        http://www.phpsense.com
   */
  
  // ------------------------------------------------------------------------
  
  class PS_Pagination {
      var $php_self;
      var $rows_per_page; //Number of records to display per page
      var $total_rows; //Total number of rows returned by the query
      var $links_per_page; //Number of links to display per page
      var $sql;
      var $debug = false;
      var $conn;
      var $page;
      var $max_pages;
      var $offset;
  
      /**
       * Constructor
       *
       * @param resource $connection Mysql connection link
       * @param string $sql SQL query to paginate. Example : SELECT * FROM users
       * @param integer $rows_per_page Number of records to display per page. Defaults to 10
       * @param integer $links_per_page Number of links to display per page. Defaults to 5
       */
  
      function PS_Pagination($connection, $sql, $rows_per_page = 1, $links_per_page = 5) {
          $this-&gt;conn = $connection;
          $this-&gt;sql = $sql;
          $this-&gt;rows_per_page = $rows_per_page;
          $this-&gt;links_per_page = $links_per_page;
          $this-&gt;php_self = htmlspecialchars($_SERVER['PHP_SELF']);
          if(isset($_GET['page'])) {
              $this-&gt;page = intval($_GET['page']);
          }
      }
  
      /**
       * Executes the SQL query and initializes internal variables
       *
       * @access public
       * @return resource
       */
      function paginate() {
          if(!$this-&gt;conn) {
              if($this-&gt;debug) echo "MySQL connection missing&lt;br /&gt;";
              return false;
          }
  
          $all_rs = @mysql_query($this-&gt;sql);
          if(!$all_rs) {
              if($this-&gt;debug) echo "SQL query failed. Check your query.&lt;br /&gt;";
              return false;
          }
          $this-&gt;total_rows = mysql_num_rows($all_rs);
          @mysql_close($all_rs);
  
          $this-&gt;max_pages = ceil($this-&gt;total_rows/$this-&gt;rows_per_page);
          //Check the page value just in case someone is trying to input an aribitrary value
          if($this-&gt;page &gt; $this-&gt;max_pages || $this-&gt;page &lt;= 0) {
              $this-&gt;page = 1;
          }
  
          //Calculate Offset
          $this-&gt;offset = $this-&gt;rows_per_page * ($this-&gt;page-1);
  
          //Fetch the required result set
          $rs = @mysql_query($this-&gt;sql." LIMIT {$this-&gt;offset}, {$this-&gt;rows_per_page}");
          if(!$rs) {
              if($this-&gt;debug) echo "Pagination query failed. Check your query.&lt;br /&gt;";
              return false;
          }
          return $rs;
      }
  
      /**
       * Display the link to the first page
       *
       * @access public
       * @param string $tag Text string to be displayed as the link. Defaults to 'First'
       * @return string
       */
      function renderFirst($tag='First') {
          if($this-&gt;page == 1) {
              return $tag;
          }
          else {
              return '&lt;a href="'.$this-&gt;php_self.'?page=1"&gt;'.$tag.'&lt;/a&gt;';
          }
      }
  
      /**
       * Display the link to the last page
       *
       * @access public
       * @param string $tag Text string to be displayed as the link. Defaults to 'Last'
       * @return string
       */
      function renderLast($tag='Last') {
          if($this-&gt;page == $this-&gt;max_pages) {
              return $tag;
          }
          else {
              return '&lt;a href="'.$this-&gt;php_self.'?page='.$this-&gt;max_pages.'"&gt;'.$tag.'&lt;/a&gt;';
          }
      }
  
      /**
       * Display the next link
       *
       * @access public
       * @param string $tag Text string to be displayed as the link. Defaults to '&gt;&gt;'
       * @return string
       */
      function renderNext($tag=' &amp;gt;&amp;gt;') {
          if($this-&gt;page &lt; $this-&gt;max_pages) {
              return '&lt;a href="'.$this-&gt;php_self.'?page='.($this-&gt;page+1).'"&gt;'.$tag.'&lt;/a&gt;';
          }
          else {
              return $tag;
          }
      }
  
      /**
       * Display the previous link
       *
       * @access public
       * @param string $tag Text string to be displayed as the link. Defaults to '&lt;&lt;'
       * @return string
       */
      function renderPrev($tag='&amp;lt;&amp;lt;') {
          if($this-&gt;page &gt; 1) {
              return '&lt;a href="'.$this-&gt;php_self.'?page='.($this-&gt;page-1).'"&gt;'.$tag.'&lt;/a&gt;';
          }
          else {
              return $tag;
          }
      }
  
      /**
       * Display the page links
       *
       * @access public
       * @return string
       */
  
  
  
  
      function renderNav() {
          for($i=1;$i&lt;=$this-&gt;max_pages;$i+=$this-&gt;links_per_page) {
              if($this-&gt;page &gt;= $i) {
                  $start = $i;
              }
          }
  
          if($this-&gt;max_pages &gt; $this-&gt;links_per_page) {
              $end = $start+$this-&gt;links_per_page;
              if($end &gt; $this-&gt;max_pages) $end = $this-&gt;max_pages+1;
          }
          else {
              $end = $this-&gt;max_pages;
          }
  
          $links = '';
          $niche = $_GET['niche'];
  
          for( $i=$start ; $i&lt;$end ; $i++) {
              if($i == $this-&gt;page) {
                  $links .= " $i ";
              }
              else {
                  $links .= ' &lt;a href="'.$this-&gt;php_self.'?page='.$i.'&amp;niche='.$niche.'"&gt;'.$i.'&lt;/a&gt; ';
              }
          }
  
          return $links;
      }
  
      /**
       * Display full pagination navigation
       *
       * @access public
       * @return string
       */
      function renderFullNav() {
          return $this-&gt;renderFirst().'&amp;nbsp;'.$this-&gt;renderPrev().'&amp;nbsp;'.$this-&gt;renderNav().'&amp;nbsp;'.$this-&gt;renderNext().'&amp;nbsp;'.$this-&gt;renderLast(); 
      }
  
      /**
       * Set debug mode
       *
       * @access public
       * @param bool $debug Set to TRUE to enable debug messages
       * @return void
       */
      function setDebug($debug) {
          $this-&gt;debug = $debug;
      }
  }
  ?&gt;
  </code></pre>