Call Nucleus Development [Nd] now, 347 688 6441 or contact us


content loading...

Code Repository

The code libraries listed to the right -> are available for public use. You may copy, distribute, and eat this code as you wish. However, the license remains in effect for the life of the software.
Pretense for download and use "[Nd] Libraries":
  • You may not use this code in a commercial setting without prior consent from [Nd].
  • You may not misrepresent yourself as the author of this code.
  • You may make changes to this code, but must make the changes available under this license.
  • These libraries are governed by their respective licenses.



<?php
 
/**
  * Paginator Class
  *
  * php 4,5
  *
  * 
  * LICENSE: Use of this library is governed by the Creative Commons License Attribution 2.5. 
  * You can check it out at: http://creativecommons.org/licenses/by/2.5/legalcode
  *
  * - You may copy, distribute, and eat this code as you wish. You must give me credit for
  * writing it. You may not misrepresent yourself as the author of this code.
  * - You may not use this code in a commercial setting without prior consent from me.
  * - If you make changes to this code, you must make the changes available under a license like
  * this one.
  *
  * @category   HTML Navigation
  * @package    PAGENAVIGATOR
  * @author     R. Fritz Washabaugh <general@nucleusdevelopment.com>
  * @copyright  2009 Nucleus Development - R. Fritz Washabaugh
  * @license    http://creativecommons.org/licenses/by/2.5/legalcode CC 2.5
  * @link       http://www.nucleusdevelopment.com.com/code/do/paginator
  *
  **/
  
// }}}
// {{{ constants
 
define('_NEXT', 'Next');
define('_PREVIOUS', 'Previous');
 
// }}}
 
/**
  * Paginator Class
  *
  * @version    Release: 1.0
  * @link        http://www.nucleusdevelopment.com.com/code/do/paginator
  * @ModDate    2009-10-21
  **/
  
// {{{ PAGENAVIGATOR
/** 
  * Paginator Class
  *
  * The class implements all function necessary for outputing html pagination.
  * Specify limits and class will return pagination navigation. 
  * 
  * This navigation was modeled after Google's own search pagination. 
  * when you specify maxpageshown limit no matter how many results pages
  * there are, and no matter what page you are on, paginator will alway
  * display maxpagesshown limit, except when total pages is less than 
  * maxpagesshown.
  *  
  *
  * // invoking the class
  *
  * PAGENAVIGATOR(LinkBase,totalNumberOfRecords, RecordsPerPage, RecordOffset,MaxPagesShown);
  *
  *    Usage example: 
  * <code>
  *include_once('PAGENAVIGATOR.php');
  *$nav = new PAGENAVIGATOR('/Photograffie/do/',$imgList->getFilesTotal(), $picPerPage, $start,10);
  *echo $nav->getNavigator();
  * </code>
  *
  * @author R. Fritz Washabaugh
  * @package PAGENAVIGATOR
  * @access public
  **/
 
 
class PAGENAVIGATOR {
      
    //{{{ members
    // (private)
    var $_pageName;
    var $_recordsPerPage;
    var $_recordOffset;
    var $_maxpagesshown;
      
    var $_totalRecords;
    var $_totalPages;
     
    // page counting 
    var $_currentPage;
    var $_startPage = 0;
    var $_endPage;
 
    var $_diffLow = 0;
    var $_diffHigh = 0;
    
    // limit total number of pages displayed
    var $_limit;
 
    //}}}
    
    //constructor
    function PAGENAVIGATOR(        $pageName, 
                                $totalRecords, 
                                $recordsPerPage, 
                                $recordOffset, 
                                $maxPagesShown=false
                            ) 
        {
        
        // set vars
        $this->_pageName = $pageName;
        $this->_recordsPerPage = $recordsPerPage;  
        $this->_maxPagesShown = $maxPagesShown;
        $this->_totalRecords = $totalRecords;
        $this->_recordsPerPage = $recordsPerPage;
        $this->_recordOffset = $recordOffset;
        
        // get total pages
        $this->_totalPages = $this->_setTotalPages($this->_totalRecords, $recordsPerPage);
        
        // find our current page
        $this->_currentPage = $this->_setCurrentPage($recordOffset, $recordsPerPage);
        
        // set max limit
        $this->_endPage = $this->_totalPages;
        
        // if a limit is set
        if(is_numeric($maxPagesShown)) {
            
            // flag for later
            $this->_limit = true;
            
            // get lower limit 
            $this->_startPage = $this->_pageMin();
            
            // get upper limit
            $this->_endPage = $this->_pageMax();
            
            // adjust upper and lower based on maxPagesShown
            $this->_setLimits();
        
        }
 
    }
 
    /**
      * Renders output
      * 
      * @access public
      **/
       
    function getNavigator(){    
           
        // init vars
        $i = 0;
        $lowerLimit = 0;
        $upperLimit = $this->_totalPages; // will display all pages
        
        $linkStr = NULL;
        $paginatation = NULL;
            
        // if we limit the pages only a certain number will show at a time.
        if($this->_limit) {
            
            $i = $this->_startPage;
            $lowerLimit = $this->_startPage;
            $upperLimit = $this->_endPage;
        
        }
                
        /**
          * do math to figure out what number should
          * be associated with what page.
          *
          **/
      
        // lets do some math
        $previousStart = $this->_recordOffset - $this->_recordsPerPage;
        $nextStart = $this->_recordOffset + $this->_recordsPerPage;
            
        do {
            //echo "current number is $i <br /> upperlimit is $upperLimit"; 
            // current record number
            $num = $this->_recordsPerPage * $i;
            
            // add link
            $paginatation .= $this->_getNumLink($i, $num, (
                                            $this->_currentPage == $i ? true : false)
                                                            );
    
        } while ( $lowerLimit < ++$i && $i < $upperLimit);
 
        if( $this->_totalRecords <= $this->_recordsPerPage )
            return $this->_prepareOutput(null);    
    
        // Form link string
        if( $this->_currentPage > 0 )  $linkStr .= $this->_getPrevious($previousStart);
        
        $linkStr .= $paginatation;
        
        if( $nextStart < $this->_totalRecords )  $linkStr .= $this->_getNext($nextStart);
        
        return $this->_prepareOutput($linkStr);    
    }
        
     /**
     * Outputs a string
     *
     * &param string $linkStr
     *
     * @access private
     */
    function _prepareOutput($linkStr) {
        
        //for debugging 
        /** "| Low Limit = $this->_diffLow <br />
        | High Limit = $this->_diffHigh <br />
        | Page Deviation = ".$this->_pageDeviation()."<br /> 
        | Start Page = $this->_startPage <br />
        | End Page = $this->_endPage<br />
        | Total Pages = $this->_totalPages<br />"
        **/
        return '<div id="pagination">'.$linkStr.'</div>';
        
    }
        
    function _getPrevious($num) {
        
        return '<a id="gleft" class="gpage active" href="'. 
                    $this->_pageName . $num . 
                    '"><strong>'._PREVIOUS.
                    '</strong></a>';
    
    }
  
    function _getNumLink( $page, $num, $current=false) {
        
        // add one because people are different than computers
        $page = $page + 1;
        
        $link = '<a class ="gpage active" href="'. 
                    $this->_pageName . $num . '">' . 
                    $page . '</a>';
        
        if($current) $link = '<div class ="gpage">'.$page.'</div>';
        
        return $link;
        
    }
  
    function _getNext($num) {
             
        return '<a id="gright" class="gpage active" href="'. 
                        $this->_pageName . $num . 
                        '"><strong>'._NEXT.
                        '</strong></a>';
     
    }
 
    // total number of pages with no limits
    function _setTotalPages($totalRecords, $recordsPerPage) {
        
        // Convert individual entry numbers to pages
        return ceil( $totalRecords / $recordsPerPage );
          
    }
 
      // Find the current and active link
    function _setCurrentPage($recordOffset, $recordsPerPage) {
        
        // find your position with relative to other pages
           return floor($recordOffset / $recordsPerPage);
          
    }
  
    function _pageDeviation() {
            
        $this->_totalRecords;
        $this->_recordsPerPage;
        $this->_recordOffset;
    
        $this->_maxPagesShown < $this->_totalPages ? 
                    $pagesShown = $this->_maxPagesShown : 
                    $pagesShown = $this->_totalPages;
        
        $limit = $this->_recordsPerPage * $pagesShown / 2; 
        
        return ceil ($limit / $this->_recordsPerPage);
        
    }
      
    /**
      * If we are limiting how many pages
         * are being shown in paginator, These two functions
      * figure the top and bottom limits
      **/
    
    function _pageMax() {
        
        // if we don't need to figure out how many pages to show
        if($this->_maxPagesShown > $this->_totalPages)
            return $this->_totalPages;
    
        $highLimit = $this->_currentPage + $this->_pageDeviation();
        
        if($highLimit < $this->_totalPages)
            return $highLimit;
        
        // init vars
        $returnLimit = $this->_totalPages;
        
        $this->_diffHigh = $highLimit - $returnLimit;
        
        return $returnLimit;
        
    }
        
    function _pageMin() {
        
        // if we don't need to figure out how many pages to show
        if($this->_maxPagesShown > $this->_totalPages)
            return 0;
    
        $lowLimit = $this->_currentPage - $this->_pageDeviation();
        
        if($lowLimit > 0)
            return $lowLimit;
        
        $this->_diffLow = -1 * $lowLimit;
        
        return 0;
    
    }
    
    // adjusts min and max based on the 'max pages shown' limit
    function _setLimits () {
        
        // init vars
        $pageMin = $this->_pageMin();
        $pageMax = $this->_pageMax();
        
        // adjust start page
        $this->_startPage = $pageMin - $this->_diffHigh;
        
        // adjust end page
        $this->_endPage = $pageMax + $this->_diffLow;
 
        return ;
    }
}
 
?>
 

URL Buffer

The following is a change log of URL updates:

©2012. All rights reserved.