Return to Snippet

Revision: 18303
at September 27, 2009 23:06 by evilwalrus


Initial Code
<?php

class Paginate_Array
{
    
    var $_array;
    var $_per_page;
    var $_lpage;
    var $_offset;

    var $_out = array();

    function Paginate_Array($array, $offset = 0, $per_page = 3)
    {
        $this->_array = $array;
        $this->_per_page = $per_page;
        $this->_lpage = ceil(sizeof($this->_array) / $this->_per_page);
        $this->_offset = $offset;
    }

    function getOffset()
    {
        return array_slice($this->_array, $this->_offset, $this->_per_page);
    }

    function getPages()
    {
        for ($i=0; $i <= ($this->_lpage - 1); $i++) {
            $this->_out[$i+1] = $i * $this->_per_page;
        }
        return $this->_out;
    }

    function _getLastOffset()
    {
        $_arr = $this->_out;
        return array_pop($_arr);
    }

    function hasPrev($text)
    {
        return ($this->_offset > 0) ? $text : '';
    }

    function hasNext($text)
    {
        return ($this->_offset < self::_getLastOffset()) ? $text : '';
    }

} // end class

?>

Initial URL


Initial Description
There are plenty of classes out there to paginate through database results, well, here's one to paginate through a static array of items.  Written quite a few years ago, recently recovered from a crashed drive.

Initial Title
Paginate through array items

Initial Tags
php, array

Initial Language
PHP