Hi I recently made this generic pagination object, so far I find it very useful coz all i have to do so far is instantiate the object,then use $page->createPage(), im posting it here so maybe you guys can give me suggestions on how to make it better and some might want to use it since it's really easy and it saves time... it would be better if others can pitch in their ideas also so we can make it even more better
Code:
<?php
class pagination
{
private $limit;//the no. of data you want to display per page
private $current_page;
private $start_row;//refers to what row you will start in your query
private $total_rows;
private $total_page;
function pagination($limit,$count_query)
{
$this->setLimit($limit);
$this->setPage();
$this->setTotalRows($count_query);
$this->setStartRow();
$this->setStartRow();
$this->setTotalPage();
}
//setters
function setLimit($limit)
{
$this->limit = $limit;
}
function setPage()
{
$this->current_page = (empty($_GET['page'])) ? 1 : $_GET['page'];
}
function setStartRow()
{
$this->start_row = $this->current_page * $this->limit - ($this->limit);
//if $limit is 5 and $current_page is 2 =( 2*5 = 10) - 5 = 5, meaning you will start at row 5
}
function setTotalRows($count_query)
{
$count_result = mysql_query($count_query) or die(mysql_error());
$this->total_rows = mysql_num_rows($count_result) or die(mysql_error());
}
function setTotalPage()
{
$this->total_page = ceil($this->total_rows / $this->limit);
}
//end of setters
//methods
function checkPrevious()//checks if there is still a previous page
{
if ($this->getCurrentPage() != 1) {
return true;
}
}
function checkNext()//checks if there is still a next page
{
if ($this->getTotalRows() - ($this->getLimit() * $this->getCurrentPage()) > 0) {
return true;
}
}
function previous()
{
return $this->getCurrentPage() - 1;
}
function next()
{
return $this->getCurrentPage() + 1;
}
function createPages($address)
{
$address = $address . "?page=";
if ($this->checkPrevious()) {
$prev_page = $address . $this->previous();
echo "<a href='{$prev_page}'> PREV </a>";
}
$ctr = 0;
$temp_address = $address;
while($ctr != $this->total_page)
{
$temp_address = $address . ++$ctr;
echo "<a href='{$temp_address}'> {$ctr} </a>";
$temp_address = $address;
}
if ($this->checkNext()) {
$next_page = $address . $this->next();
echo "<a href={$next_page}'> NEXT </a>";
}
}
//end of methods
//getters
function getLimit()
{
return $this->limit;
}
function getCurrentPage()
{
return $this->current_page;
}
function getStartRow()
{
return $this->start_row;
}
function getTotalPage()
{
return $this->total_page;
}
function getTotalRows()
{
return $this->total_rows;
}
//end of getters
}
?>
So all you have to do is instantiate and provide the proper arguments then at the pagination part just call the createPage() method and everything is good to go