Quote:
|
Originally Posted by toray
hello does any1 have a script for php paging from mysql
something like <<prev 1 2 3 Next>>
thanks
|
This one's a little more compact than the above, if you like..
It relies on the following query string variables: "page" (current page number), and "len" (the max number of records to display per page).
PHP Code:
/*****
This portion will handle calculating which records to get from the db
*****/
// If the query string variable is defined, use that. Otherwise, default to page 1
$page = isset($_GET['page']) ? $_GET['page'] : 1;
// The number of records to display per page. Defaults to 25 if not specified
$pagelength = isset($_GET['len']) ? $_GET['len'] : 25;
// The lowerbound record number to start from
$lowerbound = ($page - 1) * $pagelength;
$rs = mysql_query('SELECT * FROM mytable LIMIT '.$lowerbound.','.$pagelength) or die(mysql_error());
/****
This portion outputs the page numbers so make sure you put it where you want the text to be displayed. Also note that it's relying on the $pagelength, and $page variables from above so make sure this is same scope level
****/
$numpages = ceil(mysql_num_rows(mysql_query('SELECT * FROM mytable'))/$pagelength);
// Output the "Previous" link if you are not on the first page
echo $page > 1 ? '<a href="'.$PHP_SELF.'?page='.($page-1).'&len='.$pagelength.'">Previous</a>' : '';
// Loop through the page numbers
// Do not link the page number you are currently viewing
for ($i=1; $i <= $numpages; $i++) echo $i == $page ? $i : '<a href="'.$PHP_SELF.'?page='.$i.'&len='.$pagelength.'">'.$i.'</a>';
// Output the "Next" link if you are not on the last page
echo $page < $numpages ? '<a href="'.$PHP_SELF.'?page='.($page+1).'&len='.$pagelength.'">Next</a>' : '';