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

    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());
    }
    }
    
    ?>
      Write a Reply...