search
Homephp教程PHP源码分页类尾部页码导航

分页类——尾部页码导航  

<?php
/**
 * 生成Comment的尾部分页导航
 * @author 李俊
 *
 */
class cmtTail{
    private $currentPage;
    private $totalPage;
     
    /**
     * 生成页码导航--总控函数
     * @param string $currentPage 当前页码
     * @param string $totalPage 总页数
     * @throws Exception 页码小于1将会抛出异常
     * @return string
     */
    function __do($currentPage, $totalPage) {
        $this->currentPage=$currentPage;
        $this->totalPage=$totalPage;
        if($this->totalPage<=10){//总页数小于等于10页
            if($this->currentPage==1){//当前页是第一页
                $str=&#39;上一页&#39;.$this->currentTag();
                for ($i = 2; $i <= $this->totalPage; $i++) {
                    $str=$str.$this->commonTag($i);
                }
                $str=$str.$this->next();
            }elseif ($this->currentPage==$this->totalPage){//已跳至最后一页
                $str=$this->up();
                for ($i = 1; $i <= $this->totalPage-1; $i++) {
                    $str=$str.$this->commonTag($i);
                }
                $str=$str.$this->currentTag();
                $str=$str.$this->next();
            }else{
                $str=$this->up();
                for($i=1; $i<$this->currentPage; $i++){
                    $str=$str.$this->commonTag($i);
                }
                $str=$str.$this->currentTag();//生成当前页标签
                for ($i = $this->currentPage+1; $i <= $this->totalPage; $i++) {
                    $str=$str.$this->commonTag($i);
                }
                $str=$str.$this->next();
            }
        }elseif ($this->totalPage>10){//页码大于10
            if($this->currentPage==1){//8+2
                $str=&#39;上一页&#39;.$this->currentTag();
                for ($i = 2; $i <= 8; $i++) {
                    $str=$str.$this->commonTag($i);
                }
                $str=$str.&#39;...&#39;;//添加省略号
                $str=$str.$this->commonTag($this->totalPage-1);
                $str=$str.$this->commonTag($this->totalPage);
            }elseif($this->currentPage==$this->totalPage) {//当前为最后一页
                $str=$this->up();
                $str=$str.$this->commonTag(1);
                $str=$str.&#39;...&#39;;//添加省略号
                for ($i = $this->totalPage-6; $i < $this->totalPage; $i++) {
                    $str=$str.$this->commonTag($i);
                }
                $str=$str.$this->currentTag();
                $str=$str.$this->next();
            }else {
                if ($this->currentPage<6) {
                    $str=$this->up();
                    for ($i = 1; $i < $this->currentPage; $i++) {
                        $str=$str.$this->commonTag($i);
                    }
                    $str=$str.$this->currentTag();
                    for ($i = $this->currentPage+1; $i <= 7; $i++) {
                        $str=$str.$this->commonTag($i);
                    }
                    $str=$str.&#39;...&#39;;//添加省略号
                    $str=$str.$this->commonTag($this->totalPage);
                    $str=$str.$this->next();
                }else {
                    if ($this->currentPage>=$this->totalPage-4) {
                        $str=$this->up();
                        $str=$str.$this->commonTag(1);
                        $str=$str.&#39;...&#39;;//添加省略号
                        for ($i = $this->totalPage-6; $i < $this->currentPage; $i++) {
                            $str=$str.$this->commonTag($i);
                        }
                        $str=$str.$this->currentTag();
                        for ($i = $this->currentPage+1; $i <= $this->totalPage; $i++) {
                            $str=$str.$this->commonTag($i);
                        }
                        $str=$str.$this->next();
                    }elseif ($this->currentPage<$this->totalPage-4){//1+5+1
                        $str=$this->up();
                        $str=$str.$this->commonTag(1);
                        $str=$str.&#39;...&#39;;//添加省略号
                        $str=$str.$this->commonTag($this->currentPage-2);
                        $str=$str.$this->commonTag($this->currentPage-1);
                        $str=$str.$this->currentTag();
                        $str=$str.$this->commonTag($this->currentPage+1);
                        $str=$str.$this->commonTag($this->currentPage+2);
                        $str=$str.&#39;...&#39;;//添加省略号
                        $str=$str.$this->commonTag($this->totalPage);
                        $str=$str.$this->next();
                    }
                };
            }
        }elseif ($this->totalPage<=0){
            throw new Exception("页面不可能小于1");
        }
        return $str;
    }
    /**
     * 一般标签
     * @param int $param 页码
     * @return string
     */
    function commonTag($param) {
        return &#39;<a page="&#39;.$param.&#39;">&#39;.$param.&#39;</a>&#39;;
    }
    /**
     * 生成当前页标签
     * @param int $param 页码
     * @return string
     */
    function currentTag() {
        return &#39;<strong id="on">&#39;.$this->currentPage.&#39;</strong>&#39;;
    }
    /**
     * 生成下一页标签
     * @param int $param 下一页页码
     * @return string
     */
    function next() {
        if ($this->currentPage==$this->totalPage) {
            return &#39;下一页&#39;;
        }
        return &#39;<a page="&#39;.($this->currentPage+1).&#39;">下一页</a>&#39;;
    }
    /**
     * 生成上一页标签
     * @param int $param 上一页页码
     * @return string
     */
    function up() {
        if ($this->currentPage==1){
            return &#39;上一页&#39;;
        }else{
            return &#39;<a page="&#39;.($this->currentPage-1).&#39;">上一页</a>&#39;;
        }
    }
    /**
     * 实例化cmtTail,
     * 功能:生成Comment的尾部分页导航
     * @param string $currentPage 当前页码
     * @param string $totalPage 总页数
     * @return string 返回html标签字符串
     */
    static function GO($currentPage, $totalPage) {
        $p=new cmtTail();
        return $p->__do($currentPage, $totalPage);
    }
}

                                   

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor