<?php
/* ALOT OF THE CONFIGS CAN BE CHANGED IN THE CONSTRUCTOR */
include ("pagination.inc.php");
/*db stuff goes here */
$pgtn = new pagination;
$pgtn->sql="SELECT * FROM some_table"; //sql
$pgtn->per_page=5; //number of listings per page
$pgtn->constructor($_GET['page'], "page"); //if you wish to change your var..ie [url]www.somesite.com?page=1[/url] to [url]www.somesite.com?id=1[/url]
$limit = $pgtn->limit(); //executes query and returns result
while ($row=mysql_fetch_assoc($limit)){ //displays the result
echo $row['some_info']."<br>";
}
$pgtn->display_pages(); //displays the pagination
?>
heres the class file...
pagination.inc.php
PHP Code:
<?php
//pagination class
class pagination{
var $sql;
var $start;
var $page;
var $total_pages;
var $pagevar;
var $per_page;
var $limit_pages;
//$this->limit_pages=$this->page-$this->per_page;
//$this->limit_pages_end=$this->total_pages;
$this->limit_pages=$this->page-5; //start pages start with current page - 5
$this->limit_pages_end=$this->page+5; //end page end with current page + 5
}
function limit()
{
$result=mysql_query($this->sql." LIMIT ".$this->start.", ".$this->per_page);
return $result;
}
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>' : '';