Simple PHP Pagination Function

I created this Simple PHP Pagination Function and even though there are hundreds of such functions, this will surely be an inspiration to a beginning PHP programmer. Any form of feedback will be appreciated.

http://pastebin.com/PZdVN1T8

  1. //Usage: doPagination($totalrows,$p,$numofp);
  2. //$totalrows = The total number of rows returned by the query
  3. //$p = The current page, default would be 1
  4. //$numofp = The total number of pages (this will depend on the total rows and limit)
  5. function doPagination($totalrows,$p=1,$numofp)
  6. { ?>
  7. <div><ul><li><a href=”?” title=”reload”><?=$totalrows?> Record<?php if($totalrows>1){?>s<?php } ?></a></li>
  8. <?php if($p>2) { ?> <li><a href=”<?=PHPSELF?>?” title=”first”>&laquo; &laquo;</a></li> <?php } ?>
  9. <?php if($p>1) { ?> <li><a href=”<?=PHPSELF?>?p=<?=$p-1?>” title=”previous”>&laquo;</a></li> <?php } ?>
  10. <?php for($i = $p; $i <= $p+LIMIT; $i++) { if($i == $p) { ?> <li><a href=”#”><?=$i?></a></li> <?php } else { if($i<$numofp) { ?> <li><a href=”<?=PHPSELF?>?p=<?=$i?>”><?=$i?></a></li> <?php } }} ?>
  11. <?php if($p<$numofp) { ?> <li><a href=”<?=PHPSELF?>?p=<?=$p+1?>” title=”next”>&raquo;</a></li> <?php } ?>
  12. <?php if($p<$numofp) { ?> <li><a href=”<?=PHPSELF?>?p=<?=$numofp?>” title=”last”>&raquo; &raquo;</a></li> <?php } ?>
  13. </ul></div>
  14. <?php }

You Might Also Like