Utility PHP function to paginate SQL queries


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /**
  2. * A utility method that builds out the SQL for the LIMIT / OFFSET chunk of the SQL
  3. *
  4. * @param page the page that will be pulled, will affect the OFFSET
  5. * @param perpage how many items per page, will affect the LIMIT
  6. * @return string an SQL statement consisting of the LIMIT and OFFSET
  7. */
  8. private function _buildLimitOffsetSQL($page,$perpage) {
  9. $limit = (is_numeric($perpage) && $perpage>0) ? $perpage : 10;
  10. $offset = (is_numeric($page) && $page>0) ? ($page-1)*$perpage : 0;
  11. return "LIMIT $limit OFFSET $offset";
  12. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.