title: PHP Pagination Get Last Page
body: <p>Basically I am trying to get last page... And well it works sometimes but then say some data is entered in then it gets the wrong page and more data is entered then right page will appear I am pretty sure it has something to do with the $r part.
  Theres more to it but uh this is the main part. I think I am just using wrong math or functions please help.</p>
  
  <pre><code>$limit = 10;
  if($_GET['page'] == 0 OR $_GET['page'] == 1) {
      $offset = 0;
  } else {
      $offset = $limit * $_GET['page'] - $limit;
  }
  $next = $_GET['page'] + 1;
  $back = $_GET['page'] - 1;
  
  $stmt = $db-&gt;prepare("SELECT * FROM users");
  $stmt-&gt;execute();
  $count = $stmt-&gt;rowCount();
  
  if($count &lt; $limit) {
      $last = 1;
  }
  
  $r = $count % $limit;
  if($r &lt; 5) {
      $last = ceil($count / $limit);
  } else {
      if($r &gt;= 5) {
          $last = floor($count / $limit);
      }
  }
  
  if($_GET['page'] == 0 or $_GET['page'] == 1) {
      if (0 &gt;= $last) {
          $page = 0;
      }
      if (1 &gt;= $last) {
          $page = 1;
      }
  }
  </code></pre>