搜尋
首頁php教程PHP源码万能的分页类

万能的分页类

May 23, 2016 pm 01:07 PM


init(1000, 20);
 * $page->setNotActiveTemplate(' {a} ');
 * $page->setActiveTemplate('{a}');
 * echo $page->show();
 * 
 * 
 * @author 风居住的地方
 */
class Page {
    /**
     * 总条数
     */
    private $total;
    /**
     * 每页大小
     */
    private $pageSize;
    /**
     * 总页数
     */
    private $pageNum;
    /**
     * 当前页
     */
    private $page;
    /**
     * 地址
     */
    private $uri;
    /**
     * 分页变量
     */
    private $pageParam;
    /**
     * LIMIT XX,XX
     */
    private $limit;
    /**
     * 数字分页显示
     */
    private $listnum = 8;
    /**
     * 分页显示模板
     * 可用变量参数
     * {total}      总数据条数
     * {pagesize}   每页显示条数
     * {start}      本页开始条数
     * {end}        本页结束条数
     * {pagenum}    共有多少页
     * {frist}		首页
     * {pre}		上一页
     * {next}		下一页
     * {last}		尾页
     * {list}		数字分页
     * {goto}		跳转按钮
     */
    private $template = '共有{total}条数据每页显示{pagesize}条数据,本页{start}-{end}条数据共有{pagenum}页{frist}{pre}{list}{next}{last}{goto}';
    /**
     * 当前选中的分页链接模板
     */
    private $activeTemplate = '{text}';
    /**
     * 未选中的分页链接模板
     */
    private $notActiveTemplate = '{text}';
    /**
     * 显示文本设置
     */
    private $config = array('frist' => '首页', 'pre' => '上一页', 'next' => '下一页', 'last' => '尾页');
    /**
     * 初始化
     * @param type $total       总条数
     * @param type $pageSize    每页大小
     * @param type $param       url附加参数
     * @param type $pageParam   分页变量
     */
    public function init($total, $pageSize, $param = '', $pageParam = 'page') {
        $this->total = intval($total);
        $this->pageSize = intval($pageSize);
        $this->pageParam = $pageParam;
        $this->uri = $this->geturi($param);
        $this->pageNum = ceil($this->total / $this->pageSize);
        $this->page = $this->setPage();
        $this->limit = $this->setlimit();
    }
    
    /**
     * 设置分页模板
     * @param type $template    模板配置
     */
    public function setTemplate($template) {
        $this->template = $template;
    }
    
    /**
     * 设置选中分页模板
     * @param type $activeTemplate      模板配置
     */
    public function setActiveTemplate($activeTemplate) {
        $this->activeTemplate = $activeTemplate;
    }

    /**
     * 设置未选中分页模板
     * @param type $notActiveTemplate   模板配置
     */
    public function setNotActiveTemplate($notActiveTemplate) {
        $this->notActiveTemplate = $notActiveTemplate;
    }

    /**
     * 返回分页
     * @return type
     */
    public function show() {
        return str_ireplace(array(
            '{total}',
            '{pagesize}',
            '{start}',
            '{end}',
            '{pagenum}',
            '{frist}',
            '{pre}',
            '{next}',
            '{last}',
            '{list}',
            '{goto}',
        ), array(
            $this->total,
            $this->setPageSize(),
            $this->star(),
            $this->end(),
            $this->pageNum,
            $this->frist(),
            $this->prev(),
            $this->next(),
            $this->last(),
            $this->pagelist(),
            $this->gopage(),
        ), $this->template);
    }
    
    /**
     * 获取limit起始数
     * @return type
     */
    public function getOffset() {
        return ($this->page - 1) * $this->pageSize;
    }
    
    /**
     * 设置LIMIT
     * @return type
     */
    private function setlimit() {
        return "limit " . ($this->page - 1) * $this->pageSize . ",{$this->pageSize}";
    }

    /**
     * 获取limit
     * @param type $args
     * @return type
     */
    public function __get($args) {
        if ($args == "limit") {
            return $this->limit;
        } else {
            return null;
        }
    }

    /**
     * 初始化当前页
     * @return int
     */
    private function setPage() {
        if (!empty($_GET[$this->pageParam])) {
            if ($_GET[$this->pageParam] > 0) {
                if ($_GET[$this->pageParam] > $this->pageNum)
                    return $this->pageNum;
                else
                    return $_GET[$this->pageParam];
            }
        }
        return 1;
    }

    /**
     * 初始化url
     * @param type $param
     * @return string
     */
    private function geturi($param) {
        $url = $_SERVER['REQUEST_URI'] . (strpos($_SERVER['REQUEST_URI'], "?") ? "" : "?") . $param;
        $parse = parse_url($url);
        if (isset($parse["query"])) {
            parse_str($parse["query"], $params);
            unset($params["page"]);
            $url = $parse["path"] . "?" . http_build_query($params);
            return $url;
        } else {
            return $url;
        }
    }

    /**
     * 本页开始条数
     * @return int
     */
    private function star() {
        if ($this->total == 0) {
            return 0;
        } else {
            return ($this->page - 1) * $this->pageSize + 1;
        }
    }

    /**
     * 本页结束条数
     * @return type
     */
    private function end() {
        return min($this->page * $this->pageSize, $this->total);
    }

    /**
     * 设置当前页大小
     * @return type
     */
    private function setPageSize() {
        return $this->end() - $this->star() + 1;
    }
    
    /**
     * 首页
     * @return type
     */
    private function frist() {
        $html = '';
        if ($this->page == 1) {
            $html .= $this->replace("{$this->uri}&page=1", $this->config['frist'], true);
        } else {
            $html .= $this->replace("{$this->uri}&page=1", $this->config['frist'], false);
        }
        return $html;
    }

    /**
     * 上一页
     * @return type
     */
    private function prev() {
        $html = '';
        if ($this->page > 1) {
            $html .= $this->replace($this->uri.'&page='.($this->page - 1), $this->config['pre'], false);
        } else {
            $html .= $this->replace($this->uri.'&page='.($this->page - 1), $this->config['pre'], true);
        }
        return $html;
    }

    /**
     * 分页数字列表
     * @return type
     */
    private function pagelist() {
        $linkpage = "";
        $lastlist = floor($this->listnum / 2);
        for ($i = $lastlist; $i >= 1; $i--) {
            $page = $this->page - $i;
            if ($page >= 1) {
                $linkpage .= $this->replace("{$this->uri}&page={$page}", $page, false);
            } else {
                continue;
            }
        }
        $linkpage .= $this->replace("{$this->uri}&page={$this->page}", $this->page, true);
        for ($i = 1; $i page + $i;
            if ($page pageNum) {
                $linkpage .= $this->replace("{$this->uri}&page={$page}", $page, false);
            } else {
                break;
            }
        }
        return $linkpage;
    }

    /**
     * 下一页
     * @return type
     */
    private function next() {
        $html = '';
        if ($this->page < $this->pageNum) {
            $html .= $this->replace($this->uri.&#39;&page=&#39;.($this->page + 1), $this->config[&#39;next&#39;], false);
        } else {
            $html .= $this->replace($this->uri.&#39;&page=&#39;.($this->page + 1), $this->config[&#39;next&#39;], true);
        }
        return $html;
    }

    /**
     * 最后一页
     * @return type
     */
    private function last() {
        $html = &#39;&#39;;
        if ($this->page == $this->pageNum) {
            $html .= $this->replace($this->uri.&#39;&page=&#39;.($this->pageNum), $this->config[&#39;last&#39;], true);
        } else {
            $html .= $this->replace($this->uri.&#39;&page=&#39;.($this->pageNum), $this->config[&#39;last&#39;], false);
        }
        return $html;
    }

    /**
     * 跳转按钮
     * @return string
     */
    private function gopage() {
        $html = &#39;&#39;;
        $html.=&#39; &#39;;
        return $html;
    }

    /**
     * 模板替换
     * @param type $replace     替换内容
     * @param type $result      条件
     * @return type
     */
    private function replace($url, $text, $result = true) {
        $template = ($result ? $this->activeTemplate : $this->notActiveTemplate);
        
         $html = str_replace(&#39;{url}&#39;, $url, $template);
         $html = str_replace(&#39;{text}&#39;, $text, $html);
        return $html;
    }
}

                   

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),