PROGRAMMING SOLUTIONS

Tuesday 23 June 2009

A simple pagination object in php

Copy and paste the following code to your.php file



class PS_Pagination {
var $php_self;
var $rows_per_page; //Number of records to display per page
var $num_rows; //Total number of rows returned by the query
var $links_per_page; //Number of links to display per page
var $sql;
var $debug = false;
var $conn;
var $page;
var $max_pages;
var $offset;
var $extra_url;

/**
* Constructor
*
* @param resource $connection Mysql connection link
* @param string $sql SQL query to paginate. Example : SELECT * FROM users
* @param integer $rows_per_page Number of records to display per page. Defaults to 10
* @param integer $links_per_page Number of links to display per page. Defaults to 5
*/

function PS_Pagination($connection, $sql, $rows_per_page = 10, $links_per_page = 5, $extra_url) {
$this->conn = $connection;
$this->sql = $sql;
$this->rows_per_page = $rows_per_page;
$this->links_per_page = $links_per_page;
$this->php_self = htmlspecialchars($_SERVER['PHP_SELF']);
$this->extra_url = $extra_url;
if(isset($_GET['page'])) {
$this->page = intval($_GET['page']);
}
}

/**
* Executes the SQL query and initializes internal variables
*
* @access public
* @return resource
*/
function paginate() {
if(!$this->conn) {
if($this->debug) echo "MySQL connection missing
";
return false;
}

if($this->page <= 0) { $this->page = 1;
}

//Calculate Offset
$this->offset = $this->rows_per_page * ($this->page-1);
$this->rows_per_page1=$this->rows_per_page+1;

// check for next page
$rs = @mysql_query($this->sql." LIMIT {$this->offset}, {$this->rows_per_page1}");
# echo $this->sql." LIMIT {$this->offset}, {$this->rows_per_page1}
";

if(!$rs) {
if($this->debug) echo "Pagination query failed. Check your query.
";
return false;
} else {
$this->num_rows = mysql_num_rows($rs);
@mysql_close($rs);
}

//Fetch the required result set
$rs = @mysql_query($this->sql." LIMIT {$this->offset}, {$this->rows_per_page}");
# echo $this->sql." LIMIT {$this->offset}, {$this->rows_per_page}";

if(!$rs) {
if($this->debug) echo "Pagination query failed. Check your query.
";
return false;
}

return $rs;
}

/**
* Display the link to the first page
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to 'First'
* @return string
*/
function renderFirst($tag='<<') {
if($this->page == 1) {
return $tag;
}
else {
return ''.$tag.'';
}
}

/**
* Display the link to the last page
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to 'Last'
* @return string
*/
function renderLast($tag='>>') {
if($this->page == $this->max_pages) {
return $tag;
}
else {
return ''.$tag.'';
}
}

/**
* Display the next link
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to '>>'
* @return string
*/
function renderNext($tag=' >') {
if($this->num_rows > $this->rows_per_page) {
return ''.$tag.'';
}
else {
return $tag;
}
}

/**
* Display the previous link
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to '<<' * @return string */ function renderPrev($tag='<') { if($this->page > 1) {
return ''.$tag.'';
}
else {
return $tag;
}
}

/**
* Display the page links
*
* @access public
* @return string
*/
function renderNav() {
for($i=1;$i<=$this->max_pages;$i+=$this->links_per_page) {
if($this->page >= $i) {
$start = $i;
}
}

if($this->max_pages > $this->links_per_page) {
$end = $start+$this->links_per_page;
if($end > $this->max_pages) $end = $this->max_pages+1;
}
else {
$end = $this->max_pages;
}

$links = '';

for( $i=$start ; $i<$end ; $i++) { if($i == $this->page) {
$links .= " $i ";
}
else {
$links .= ' '.$i.' ';
}
}

return $links;
}

/**
* Display full pagination navigation
*
* @access public
* @return string
*/
function renderFullNav() {
return $this->renderFirst().' '.$this->renderPrev().' '.$this->renderNav().' '.$this->renderNext().' '.$this->renderLast();
}

/**
* Set debug mode
*
* @access public
* @param bool $debug Set to TRUE to enable debug messages
* @return void
*/
function setDebug($debug) {
$this->debug = $debug;
}
}
?>





How to init the pagination object in your php code:
// Create a Pagination object
$pager = new PS_Pagination($con, $strSql, $_MAX_ROWS, $_MAX_LINKS, $q_params);
// The paginate() function returns a mysql result set
$pages = $pager->paginate();

Where $con is your connection string,
$strSql is your select statement, example: "select * from employees"
$_MAX_ROWS = max rows per page to show.
$_MAX_LINKS = max links per page to show.
$q_params = other options that you might want to have in your request for having the control of the pages.



Use the following commands to move between the pages

$pager->renderFirst(); // go to the the first page
$pager->renderPrev(); // go to the previous page
$pager->renderNext(); // go to the next page
$pager->renderLast(); // go to the last page

1 comment:

  1. Great!!This class works very good on my code!!Good work!

    ReplyDelete