Posted By

cburyta on 03/20/08


Tagged

sql php textmate


Versions (?)

Who likes this?

11 people have marked this snippet as a favorite

wbowers
axthos
sonix
paullorentzen
tcmacdonald
vali29
nb109
Hollow
tapdrup
mecha
ringo380


Utility PHP function to paginate SQL queries


 / Published in: PHP
 

  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  

You need to login to post a comment.