一个完整的分页类

来源:互联网 发布:闪迪u盘数据恢复软件 编辑:程序博客网 时间:2024/05/17 22:57
<?php

/**
 * Pager class.
 *
 * @package     common
 * @author      Rational.Li
 * @version     1.0
 */
class Pager {
    var $url;
    var $countall;
    var $page;
    var $thestr;
    var $backstr;
    var $nextstr;
    var $currentPageNo;
    var $countlist;
    var $style;
    
    function Pager($countall, $countlist, $style='page') {
        global $lang_common;
        $this->currentPageNo = '1';
        $this->countall = $countall;
        $this->countlist = $countlist;
        $this->style = $style;
        if ($this->countall % $this->countlist != 0) {
            $this->page = sprintf("%d", $this->countall / $this->countlist) + 1;
        } else {
            $this->page = $this->countall / $this->countlist;
        }
        
        $this->currentPageNo = $_GET["currentPageNo"];
        if (!preg_match('/^[1-9][0-9]*$/', $this->currentPageNo) || empty($this->currentPageNo)) {
            $this->currentPageNo = 1;
        }

        if ($this->currentPageNo > $this->page) {
            $this->currentPageNo = $this->page;
        }

        $this->url = Pager::getUrl();
        if (isset($_GET["currentPageNo"]) && $_GET["currentPageNo"] != $this->currentPageNo) {
            $this->url = str_replace("&currentPageNo=" . $_GET["currentPageNo"], "&currentPageNo=$this->currentPageNo", $this->url);
        }

        if ($this->page <= 10) {
            for ($i = 1; $i < $this->page + 1; $i++) {
                $this->thestr = $this->thestr . Pager::makecurrentPageNo($i, $this->currentPageNo);
            }
        } else {
            if ($this->currentPageNo <= 5) {
                for ($i = 1; $i < 10; $i++) {
                    $this->thestr = $this->thestr . Pager::makecurrentPageNo($i, $this->currentPageNo);
                }
            } else {
                if (6 + $this->currentPageNo <= $this->page) {
                    for ($i = $this->currentPageNo - 4; $i < $this->currentPageNo + 6; $i++) {
                        $this->thestr = $this->thestr . Pager::makecurrentPageNo($i, $this->currentPageNo);
                    }
                } else {
                    for ($i = $this->currentPageNo - 4; $i < $this->page + 1; $i++) {
                        $this->thestr = $this->thestr . Pager::makecurrentPageNo($i, $this->currentPageNo);
                    }
                }
            }
        }

        $this->backstr = Pager::gotoback($this->currentPageNo);
        $this->nextstr = Pager::gotonext($this->currentPageNo, $this->page);
        $this->pernum = Pager::per_num();

    
    function tostring() {
        global $lang_common;
        return ("<font size=2>".$this->pernum . $this->backstr . $this->thestr . $this->nextstr . " ($lang_common[total] $this->countall $lang_common[records])</font>");
    }

    function makecurrentPageNo($i, $currentPageNo) {
        if ($i == $currentPageNo) {
            return " <font class='" . $this->style . "'>" . $i . "</font>";
        } else {
            return " <a href=" . Pager::replacecurrentPageNo($this->url, 5, $i) . " class='" . $this->style . "'><u>" . $i . "</u></a>";
        }
    }
    
    function per_num() {
        global $lang_common;
        $current_url = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"];
        if ($_GET['search']) {
            $indentity = strpos($current_url, '?') === false ? '?' : '&';
            $current_url .= $indentity . $_SERVER["QUERY_STRING"];
        }
        
        $per_num = intval($_GET['per_num']);
        $str = $lang_common['each_num'];
        $str .= "<select onchange=\"alter_per_num('$current_url',this.value)\">";
        $str .= "<option value=\"10\" ". ($per_num === 10 ? 'selected' : '') .">10</option>";
        $str .= "<option value=\"20\" ". ($per_num === 20 ? 'selected' : '') .">20</option>";
        $str .= "<option value=\"30\" ". (($per_num === 30 or $per_num === 0) ? 'selected' : '') .">30</option>";
        $str .= "<option value=\"40\" ". ($per_num === 40 ? 'selected' : '') .">40</option>";
        $str .= "</select>".$lang_common['item']."&nbsp;&nbsp;&nbsp;&nbsp;";
        return $str;
    }

    function gotoback($currentPageNo) {
        global $lang_common;
        $first = $lang_common['first_page'];
        $prev = $lang_common['prev_page'];

        if ($currentPageNo - 1 > 0) {
            return $this->gotoback = " <a href=" . Pager::replacecurrentPageNo($this->url, 3, 0) . " class='" . $this->style . "'>$first</a> | <a href=" . Pager::replacecurrentPageNo($this->url, 2, 0) . " class='" . $this->style . "'>$prev</a> | ";
        } else {
            return $this->gotoback = "<span class='" . $this->style . "'>$first | $prev | </span> ";
        }
    }

    function gotonext($currentPageNo, $page) {
        global $lang_common;
        $next = $lang_common['next_page'];
        $last = $lang_common['last_page'];

        if ($currentPageNo < $page) {
            return " | <a href=" . Pager::replacecurrentPageNo($this->url, 1, 0) . " class='" . $this->style . "'>$next</a> | <a href=" . Pager::replacecurrentPageNo($this->url, 4, 0) . " class='" . $this->style . "'>$last</a>";
        } else {
            return " <span class='" . $this->style . "'> | $next | $last</span>";
        }
    }

    function replacecurrentPageNo($url, $flag, $i) {
        if ($flag == 1) {
            $temp_currentPageNo = $this->currentPageNo;
            return str_replace("currentPageNo=" . $temp_currentPageNo, "currentPageNo=" . ($this->currentPageNo + 1), $url);
        } else if ($flag == 2) {
            $temp_currentPageNo = $this->currentPageNo;
            return str_replace("currentPageNo=" . $temp_currentPageNo, "currentPageNo=" . ($this->currentPageNo - 1), $url);
        } else if ($flag == 3) {
            $temp_currentPageNo = $this->currentPageNo;
            return str_replace("currentPageNo=" . $temp_currentPageNo, "currentPageNo=1", $url);
        } else if ($flag == 4) {
            $temp_currentPageNo = $this->currentPageNo;
            return str_replace("currentPageNo=" . $temp_currentPageNo, "currentPageNo=" . $this->page, $url);
        } else if ($flag == 5) {
            $temp_currentPageNo = $this->currentPageNo;
            return str_replace("currentPageNo=" . $temp_currentPageNo, "currentPageNo=" . $i, $url);
        } else {
            return $url;
        }
    }

    function getUrl() {
        $url = "http://" . $_SERVER["HTTP_HOST"];
        if (isset($_SERVER["REQUEST_URI"])) {
            $url .= $_SERVER["REQUEST_URI"];
        } else {
            $url .= $_SERVER["PHP_SELF"];
            if (!empty($_SERVER["QUERY_STRING"])) {
                $url .= "/" . $_SERVER["QUERY_STRING"];
            }
        }

        if (!preg_match("/(currentPageNo=|CurrentPageNo=|CurrentpageNo=|Currentpageno=)/", $url)) {
            if (!strpos($url, "?")) {
                $url = $url . "?currentPageNo=1";
            } else {
                $url = $url . "&currentPageNo=1";
            }
        }
        return $url;
    }

}

?>


在html中的调用:

    <div id="pagernation">
        <?php $pager = new Pager($total_count, $page_size, "page"); ?>
    </div>


原创粉丝点击