PHP単純ページングクラス


<?php
/**
 *    
 * Created by Glex
 * Date:    2013-02-01
 * example: $pager = new Pager(100, 10);
 *          $pager->showPager();
 */
final class Pager{
    private $rowSum;    //     
    private $row;       //      
    private $pageSum;   //    
    private $page;		//    
    private $lang;		//   
    private $url;
    private $cn = array(
        'records' => '   ',
        'pages' => ' ',
        'first' => '  ',
        'pre' => '   ',
        'next' => '   ',
        'last' => '  '
    );
    private $en = array(
        'records' => 'Records',
        'pages' => 'Pages',
        'first' => 'First',
        'pre'   => 'Pre',
        'next'  => 'Next',
        'last'  => 'Last'
    );

    /**
     *     
     */
    function __construct($rowSum, $row = 15, $lang = 'cn', $url = '') {
        $this->rowSum = $rowSum;
        $row = ($row < 1) ? (1) : ($row);
        $this->row = $row;
        $this->pageSum = ceil($rowSum / $row);
        $this->page = $this->getPage();
        $this->lang = $this->{$lang};
        if ($url) { $this->url = $url; }
        else { $this->url = (isset($_SERVER['REQUEST_URI'])) ? ($_SERVER['REQUEST_URI']) : ($_SERVER['HTTP_X_REWRITE_URL']); }
    }

    /**
     *   sql
     */
    function limit() {
        $start = ($this->page - 1) * $this->row;
        $row = $this->row;
        return " LIMIT $start, $row";
    }

    /**
     *      
     */
    private function getPage() {
        $page = (isset($_GET['gotopage']) && $_GET['gotopage'] > 1) ? (ceil($_GET['gotopage'])) : (1);
        if ($this->pageSum && $this->pageSum < $page) $page = $this->pageSum;
        return $page;
    }

    /**
     *     HTML
     * @param int $showtext        0   1  
     * @param int $shownum        0   1  
     * @param int $showlvtao               
     * @param int $showjump       0   1  
     * @param int $showselect       0   1  
     * @param int $showselects              
     */
    function showPager($showtext = 1, $shownum = 1, $showlvtao = 7, $showjump = 1, $showselect = 0, $showselects = 10)
    {
        $rowSum = $this->rowSum;
        $row = $this->row;
        $pageSum=$this->pageSum;
        $page = $this->page;
        $page = min($pageSum, $page);
        $lang = $this->lang;
        $html = '<div id="nice-pager">';

        // url
        $url = $this->url;
        $parse = parse_url($url);
        if (isset($parse['query'])) {
            parse_str($parse['query'], $params);
            $params['gotopage'] = '';
            $url = $parse['path'] . '?' . http_build_query($params);
        }
        else{ $url.='?gotopage='; }

        //     
        if ($showtext) {
            $start = ($rowSum) ? (($page - 1) * $row + 1) : (0);
            $end = min($page * $row, $rowSum);
            $html .= "<span>{$start}-{$end}/{$rowSum}{$lang['records']}</span><span>{$page}/{$pageSum}{$lang['pages']}</span>";
        }

        if ($pageSum <= 1) return print $html . '</div>';

        //   、   
        $prev = $page - 1;
        $html .= ($prev) ? ('<a href=' . $url . '1>' . $lang['first'] . '</a>') : ('<span class="disabled">' . $lang['first'] . '</span>');
        $html .= ($prev) ? ('<a href=' . $url . $prev . '>' . $lang['pre'] . '</a>') : ('<span class="disabled">' . $lang['pre'] . '</span>');

        //       
        if ($shownum) {
            $o = $showlvtao;
            $u = ceil($o / 2);
            $f = $page - $u;
            if ($f < 0) { $f = 0; }
            $n = $pageSum;
            if ($n < 1) { $n = 1; }

            $html .= ($page == 1) ? ('<span class="current">1</span>') : ("<a href='{$url}1'>1</a>"); //  1     
            for($i=1;$i<=$o;$i++){
                if ($n <= 1) break;

                $c = $f + $i;
                if ($i == 1 && $c > 2) { $html .= '...'; }
                if ($c == 1) continue;
                if ($c == $n) break;

                $html .= ($c == $page) ? ('<span class="current">' . $page . '</span>') : ("<a href='{$url}{$c}'>{$c}</a>");
                if ($i == $o && $c < $n - 1) { $html .= '...'; }
                if ($i > $n) break;
            }
            $html .= ($page == $n && $n != 1) ? ('<span class="current">' . $n . '</span>') : ("<a href='{$url}{$n}'>{$n}</a>"); //   1     
        }

        //    、  
        $next = ($page == $pageSum) ? (0) : ($page + 1);
        $html .= ($next) ? ("<a href='{$url}{$next}'>{$lang['next']}</a>") : ("<span class='disabled'>{$lang['next']}</span>");
        $html .= ($next) ? ("<a href='{$url}{$pageSum}'>{$lang['last']}</a>") : ("<span class='disabled'>{$lang['last']}</span>");

        //       
        if ($showjump) {
            $html .= "<span>  <input type='text' class='to-page' size='3' maxlength='3' title='       ' onkeyup=\"this.value=this.value.replace(/\D/g,'')\" onafterpaste = \"this.value = this.value.replace(/\D/g,'')
\" onkeydown = \"javascript:if(event.charCode==13||event.keyCode==13){if(!isNaN(this.value)){document.location.href='$url'+this.value+'';}return false;}\"/>{$lang['pages']}</span>";
        }

        //         
        if ($showselect) {
            $html .= "<span>  <select name='topage' onchange='window.location=\"$url\"+this.value'>
"; $lvtao = $page - $showselects / 2; $lvtaos = ($lvtao <= 0) ? (1) : ($lvtao); $lvtaoe = $page + $showselects / 2; if ($page < $showselects / 2 && $showselects <= $pageSum) { $lvtaoe = $showselects; } else if ($lvtaoe >= $pageSum) { $lvtaoe = $pageSum; } for ($i = $lvtaos; $i <= $lvtaoe; $i++) { $html .= ($i == $page) ? ("<option value='$i' selected>$i</option>
") : ("<option value='$i'>$i</option>
"); } $html .= "</select>{$lang['pages']}</span>"; } return print $html . '</div>'; } } ?>
<style type="text/css">
    #nice-pager { padding: 10px 0; font-size: 13px; }
    #nice-pager span { margin: 0; padding:3px 6px 2px; }
    #nice-pager span.disabled { margin: 0 3px; }
    #nice-pager span.current,#nice-pager span.disabled { background-color: #E2E2E2; border: solid 1px #808080; border-radius: 5px; }
    #nice-pager a { padding: 3px 6px 2px; margin: 0 3px; background-color: #000; color:white; border: solid 1px #808080; border-radius: 5px; text-decoration: none; }
    #nice-pager a:hover { background-color: #E2E2E2; color:#000; }
    #nice-pager input.to-page { width:35px; border-radius: 6px; }
</style>