Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Pagination with PHP and MySQL
- $rowsperpage = 10;
- $totalpages = ceil($numrows / $rowsperpage);
- // get the current page or set a default
- if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { $currentpage = (int) $_GET['currentpage']; }
- else { $currentpage = 1; }
- // if current page is greater than total pages...
- if ($currentpage > $totalpages) { $currentpage = $totalpages; }
- // if current page is less than first page...
- if ($currentpage < 1) { $currentpage = 1; }
- // the offset of the list, based on current page
- $offset = ($currentpage - 1) * $rowsperpage;
- // run the query
- $sql = "SELECT * FROM table WHERE keyword LIMIT $offset, $rowsperpage";
- $result = mysql_query($sql, $link) or trigger_error("SQL", E_USER_ERROR);
- // while there are rows to be fetched...
- while ($list = mysql_fetch_assoc($result))
- {
- //echo data for each record here
- }
- // building pagination links
- $range = 3;
- // if not on page 1, don't show back links
- if ($currentpage > 1) {
- // show << link to go back to page 1
- echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
- // get previous page num
- $prevpage = $currentpage - 1;
- // show < link to go back to 1 page
- echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
- } // end if
Advertisement
Add Comment
Please, Sign In to add comment