It looks like you need to refine the public interface. All of the setting up work is done in the constructor, so you may actually just want to provide the constructor, and then make the object immutable by making all the setters/mutators private. This makes sense especially since setting the limit without also recalculating the start row and total number of pages would leave the object in an inconsistent state.
Other possible improvements include:
1. Renaming $total_rows and $total_page to $row_count and $page_count respectively.
2. Removing the duplicate call to setStartRow() in the constructor.
3. Checking that the page given is valid before actually setting the current page.
4. Requiring a COUNT query instead of using mysql_num_rows().
4. Having checkPrevious() and checkNext() properly return boolean values on all possible execution paths. Renaming them to hasPrevious() and hasNext() may be a good idea too.
5. Making createPages() more robust, e.g., return an array of links so the caller can better decide how to display them. I would rename it to createPageList() and remove the creation of the previous and next links. These links can be created by using other member functions such as hasPrevious(), hasNext(), previous() and next(). Incidentally, when there are many pages, the programmer may want to only list a few pages. Returning an array allows for this flexibility, though createPageList() could be augmented to include such functionality.
This would be my possible result after improvement:
<?php
class Pagination
{
private $limit; // maximum number of items per page
private $current_page = 1;
private $start_row; // offset of first row to retrieve
private $row_count; // total number of rows
private $page_count; // total number of pages
/* Public Interface */
function Pagination($count_query, $limit = 10)
{
$this->setLimit($limit);
$this->countRows($count_query);
$this->countPages();
$this->initUserDefinedPage();
$this->computeStartRow();
}
function hasPrevious()
{
return $this->getCurrentPage() > 1;
}
function hasNext()
{
return $this->getRowCount() > ($this->getLimit() * $this->getCurrentPage());
}
function previous()
{
return $this->getCurrentPage() - 1;
}
function next()
{
return $this->getCurrentPage() + 1;
}
function createPageList($address)
{
// Address assumed to contain '?' or '&', as appropriate.
$address .= 'page=';
$ret = array();
for ($page = 1; $page <= $this->getPageCount(); ++$page)
{
if ($page != $this->getCurrentPage())
{
$ret[] = '<a href="' . $address . $page . '">' . $page . '</a>';
}
else
{
$ret[] = $page;
}
}
return $ret;
}
function getCurrentPage()
{
return $this->current_page;
}
function getLimit()
{
return $this->limit;
}
function getPageCount()
{
return $this->page_count;
}
function getRowCount()
{
return $this->row_count;
}
/* Private Implemention */
private function getStartRow()
{
return $this->start_row;
}
private function setLimit($limit)
{
$this->limit = $limit;
}
private function isValidPage($page)
{
return ctype_digit($page) && $page > 0 && $page <= $this->getPageCount();
}
private function initUserDefinedPage()
{
if (!empty($_GET['page']) && $this->isValidPage($_GET['page']))
{
$this->current_page = $_GET['page'];
}
}
private function computeStartRow()
{
// e.g., $limit = 5, $current_page = 2,
// $start_row = 5 * (2 - 1) = 5
$this->start_row = $this->getLimit() * ($this->getCurrentPage() - 1);
}
private function countRows($count_query)
{
$this->row_count = mysql_result(mysql_query($count_query), 0);
}
private function countPages()
{
$this->page_count = ceil($this->getRowCount() / $this->getLimit());
}
}
?>