title: How to get MySQL data to update once "next" button is pressed?
body: <p>I cannot seem to understand why this pagination with PHP &amp; MySQL isn't switching data to the next page once the button is pressed, even though on the header it goes to the next page ex: site.com/index.php?page=2</p>
  
  <pre><code>postClass {
  
      //the __construct is on the class as well, just showing what isn't working under it.
  
      private $perPage = 8;
      private $startPage = 0;
  
      public function latestPosts(){
          $this-&gt;db-&gt;setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);
          $sth = $this-&gt;db-&gt;prepare("SELECT * FROM posts ORDER BY id DESC LIMIT ?, ?");
          $sth-&gt;execute(array($this-&gt;startPage, $this-&gt;perPage));
  
          $data = $sth-&gt;fetchAll();
          return $data;
      }
  
      public function getPerPage(){ 
        return $this-&gt;perPage;
      }
  
      public function getStartPage(){ 
        return $this-&gt;startPage;
      }
  }
  </code></pre>
  
  <p>and on another php file I am using the following to show the data</p>
  
  <pre><code>$startPage  = $postClass-&gt;getStartPage();
  $perPage    = $postClass-&gt;getPerPage();
  
  if(!isset($_GET['page']))
  {
      $page = 1;
  } else {
      $page = $_GET['page'];
  }
  
  if($page &lt;= 1)
  {
      $startPage = 0;
  } else {
      $startPage = $page * $perPage - $perPage;
  } 
  
  foreach($latestPosts as $post)
  {
      $title = $post['title'];
  
      echo ''.$title.'';
  }
  
  $prev = $page-1;
  $next = $page+1;
  
  echo "&lt;a href='?page=$prev'&gt;previous&lt;/a&gt; &lt;a href='?page=$next'&gt;next&lt;/a&gt;";
  </code></pre>
  
  <p>I have tried print_r &amp; var_dump to see what it prints but it seems to work fine, just that the data doesn't seem to be passing into the if(page) statement, from what I can think of, but I'm no expert otherwise it would work.</p>
  
  <p>Here is what print_r &amp; var_dump shows.</p>
  
  <pre><code>var_dump($perPage) = int 8
  print_r($perPage) = 8
  
  var_dump($startPage) = int 0
  print_r($startPage) = 0
  </code></pre>
  
  <p>I don't understand why once I press the next button the data(8 first results) doesn't switch to the next 8 results. </p>
  
  <h2>other failed attempts</h2>
  
  <p>I have also tried this which I figured would work but I couldn't pass the variable to the other functions..</p>
  
  <pre><code>public function latestPosts(){
      $perPage = 8;
      $startPage = 0;
  
      $this-&gt;db-&gt;setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);
      $sth = $this-&gt;db-&gt;prepare("SELECT * FROM articles ORDER BY article_uid DESC LIMIT ?, ?");
      $sth-&gt;execute(array($startPage, $perPage));
  
      $data = $sth-&gt;fetchAll();
      return $data;
  }
  
  public function getPerPage(){ 
      eturn $this-&gt;latestPosts($perPage);
  }
  
  public function getStartPage(){ 
      return $this-&gt;latestPosts($startPage);
  }
  </code></pre>
  
  <p>then did the same thing on the <code>index.php</code> page but errors were giving that I couldn't figure out how to pass those variables.</p>