Cel:

Pager (zwany także "paginator") - ogólna nazwa rozwiązania pozwalajacego na dzielenie wyników wyszukiwania, listowania bądź też samego tekstu na kolejne strony z możliwością poruszania się pomiędzy poszczególnymi podstronami.

Poniższy przykład demonstruje klasę do generowania i osbługi pagera. Styl CSS użyty w demonstracji opisany został w oddzielnym artykule: pager - styl CSS.

Końcowy efekt:


Kod dla przykład:


<?
    $config 
= array(
        
'items'       =>  200,
        
'per_page'    =>  10,
        
'base_url'    =>  '?a=b',
        
'param'       =>  '&page',
        
'current'     =>  (isset($_GET['page']) ? (int)$_GET['page'] : 0),
        
'spread'      =>  6,
    );


    
$pager = new pager($config);

    echo 
'<ul class="pager">'."\n";
    foreach(
$pager->pages as $item) {
        if(isset(
$item['is_prev'])) {
            echo 
'<li class="prev"><a href="'.$item['url'].'">&laquo; Poprzednia strona</a></li>'."\n";
        }
        else if(isset(
$item['is_next'])) {
            echo 
'<li class="prev"><a href="'.$item['url'].'">Następna strona &raquo;</a></li>'."\n";
        }
        else if(isset(
$item['is_dots'])) {
            echo 
'<li class="empty">...</li>'."\n";
        }
        else {
            echo 
'<li><a href="'.$item['url'].'"'.($item['active'] == true ' class="active"':'').'>'.$item['nr'].'</a></li>'."\n";
        }
    }
    echo 
'</ul>';

Klasa pager:


<?
class pager {

    var 
$pages = array();

    function 
__construct($params) {
        
/**
         * Całkowita ilość wyników
         * Total number of items
         */
        
$this->items         =    $params['items'];
        
/**
         * Ilość wyników do pokazania na jedną strone
         * Number of items to store on single page
         */
        
$this->per_page        =    $params['per_page'];
        
/**
         * Bazowy adres strony
         * Base URL to the page
         */
        
$this->base_url        =    (isset($params['base_url'])) ? $params['base_url'] : null;
        
/**
         * Parametr używany do przechowyania wskaźnika, domyślnie `?page`
         * Parameter used for paging, `?page` by default
         */
        
$this->param        =    (isset($params['param'])) ? $params['param'] : '?page';
        
/**
         * Aktualnie wybrana strona
         * Current page number
         */
        
$this->current        =    (isset($params['current'])) ? $params['current'] : ((isset($_GET[$this->param])) ? $_GET[$this->param] : 0);
        
/**
         * Rozrzut - pokaże tylko +/-($spread/2) linków od aktualnej strony
         * Spread - show only +/-($spread/2) links from $current
         */
        
$this->spread        =    (isset($params['spread'])) ? $params['spread'] : null;

        
$this->generate();
    }

    
/**
     * Konstruktor kompatybilny dla PHP4
     * PHP 4 compatible constructor
     *
     * @param unknown_type $params
     * @return pager
     */
    
function pager($params) {
        
$this->__construct($params);
    }

    
/**
     * Generuje linki dla kolejnych stron
     * Generates links for next/preious pages
     */
    
function generate() {
        
$pages ceil($this->items $this->per_page);
        if(
$this->current 0) {
            
$this->pages[] = array(
                
'url'        =>    (($this->base_url != null) ? $this->base_url '').$this->param.'='.($this->current 1),
                
'is_prev'    =>    true,
            );
        }
        if(
$this->spread) {
            
$start        $this->current ceil($this->spread 2);
            
$end        $this->current ceil($this->spread 2);

            if(
$start 0$start 0;
            else {
                
$this->pages[] = array(
                    
'nr'    =>    1,
                    
'url'    =>    (($this->base_url != null) ? $this->base_url '').$this->param.'=0',
                );
                
$this->pages[] = array('is_dots'=>true);
            }
            if(
$end $pages$end $pages;
        }
        else {
            
$start 0;
            
$end ceil($this->items $this->per_page);
        }
        for(
$p $start $p <= $end $p++) {
            
$this->pages[] = array(
                
'nr'        =>    ($p+1),
                
'url'        =>    (($this->base_url != null) ? $this->base_url '').$this->param.'='.$p,
                
'active'    =>    ($p == $this->current) ? true false,
            );
        }
        if(
$end < ($pages 1)) {
            
$this->pages[] = array('is_dots'=>true);
            
$this->pages[] = array(
                
'nr'    =>    ($pages 1),
                
'url'    =>    (($this->base_url != null) ? $this->base_url '').$this->param.'='.$pages,
            );
        }
        if(
$this->current $pages) {
            
$this->pages[] = array(
                
'url'        =>    (($this->base_url != null) ? $this->base_url '').$this->param.'='.($this->current 1),
                
'is_next'    =>    true,
            );
        }
    }
}

O autorze:

Zaprezentowany powyżej skrypt / styl CSS lub inna informacja pochodzi z blogu EpicWeb.pl i powstał na podstawie przemyśleń własnych autora lub pod wpływem artykułów z innych źródeł (podane zostało wtedy ono na blogu w odpowiednim poście). Jeżeli nie zaznaczono inaczej, udostępniany jest na zasadach CC-BY-SA 2.5.

Autorem jest Bartosz "BTM" Szczeciński - programista PHP, MySQL, lubiący eksperymentować ze stylami CSS, JavaScript oraz szeroko pojętą tematyką aplikacji internetowych. Możesz skontaktować się ze mną poprzez email btm@anfo.pl lub Jabbera btm@jabber.aster.pl