Guest User

Untitled

a guest
Jun 26th, 2012
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. Pagination with PHP and MySQL
  2. $rowsperpage = 10;
  3. $totalpages = ceil($numrows / $rowsperpage);
  4.  
  5. // get the current page or set a default
  6. if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { $currentpage = (int) $_GET['currentpage']; }
  7. else { $currentpage = 1; }
  8.  
  9. // if current page is greater than total pages...
  10. if ($currentpage > $totalpages) { $currentpage = $totalpages; }
  11.  
  12. // if current page is less than first page...
  13. if ($currentpage < 1) { $currentpage = 1; }
  14.  
  15. // the offset of the list, based on current page
  16. $offset = ($currentpage - 1) * $rowsperpage;
  17.  
  18. // run the query
  19. $sql = "SELECT * FROM table WHERE keyword LIMIT $offset, $rowsperpage";
  20. $result = mysql_query($sql, $link) or trigger_error("SQL", E_USER_ERROR);
  21. // while there are rows to be fetched...
  22. while ($list = mysql_fetch_assoc($result))
  23. {
  24. //echo data for each record here
  25. }
  26.  
  27. // building pagination links
  28. $range = 3;
  29.  
  30. // if not on page 1, don't show back links
  31. if ($currentpage > 1) {
  32. // show << link to go back to page 1
  33. echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
  34. // get previous page num
  35. $prevpage = $currentpage - 1;
  36. // show < link to go back to 1 page
  37. echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
  38. } // end if
Advertisement
Add Comment
Please, Sign In to add comment