收藏一个php用的一个页码按钮类

调用

$pagenav = new pagenav();
$pagenav -> pageinit(页面地址无参数的,总页数,所在页面,除1和总数中间的数量,我测试为7);

PHP code

class pagenav{

    public $pageurl;
    public $totalpage;
    public $inpage;
    public $navcenter;

    function    pageinit($pageurl,$totalpage,$inpage,$navcenter){
        $this->pageurl = $pageurl;
        $this->totalpage = $totalpage;
        $this->inpage = $inpage;
        $this->navcenter = $navcenter;
        
        echo "<strong style='border: 1px #7099cc solid ;padding: 5px;line-height: 30px;height: 30px;padding-left: 15px;margin-right: 10px'>翻页 » </strong>";
        $this->showbof();
        $this->showcenter();
        $this->showeof();
        
    }

    function    showbof(){
        if($this->inpage!=1){
            echo "<a href='";
            echo $this->pageurl;
            echo "' class='pagenavoff'>";
            echo 1;
            if($this->inpage > ceil($this->navcenter / 2) + 1 and $this->totalpage > 9){
                echo "...";
            }
            echo "</a>";
        }else{
            echo "<strong class='pagenavon'>1</strong>";
        }
    }
    function    showcenter(){
        $center_bofnum = $this->inpage - ceil($this->navcenter / 2) + 1;
        if($this->inpage - ceil($this->navcenter / 2) < 1){
            $center_bofnum = 2;
        }
        $center_eofnum = $center_bofnum + $this->navcenter;
        if( $center_eofnum > $this->totalpage - 1){
            $center_eofnum = $this->totalpage ;
        }
        if( $center_eofnum - $center_bofnum < 7 and $this->inpage - ceil($this->navcenter / 2) > 2){ 
            $center_bofnum = $center_eofnum - $this->navcenter;
        }

        while($center_bofnum < $center_eofnum ){
            echo " ";
if($this->inpage!=$center_bofnum){ echo "<a href='"; echo $this->pageurl."?page=".$center_bofnum; echo "' class='pagenavoff'>"; echo $center_bofnum; echo "</a>"; }else{ echo "<strong class='pagenavon'>"; echo $center_bofnum; echo "</strong>"; } $center_bofnum++; } } function showeof(){ if($this->totalpage > 1){ echo " ";
if($this->inpage!=$this->totalpage){ echo "<a href='"; echo $this->pageurl."?page=".$this->totalpage; echo "' class='pagenavoff'>"; if($this->totalpage - $this->inpage >= ceil($this->navcenter / 2) + 1 and $this->totalpage > 9){ echo "..."; } echo $this->totalpage; echo "</a>"; }else{ echo "<strong class='pagenavon'>".$this->totalpage."</strong>"; } } } }

CSS

.pagenavoff{text-align: center;border: 1px #7099cc solid ;padding: 5px;padding-left: 12px;padding-right: 12px}
.pagenavoff:link{color:#;font-weight:normal;text-decoration:none}
.pagenavoff:visited{color:#;font-weight:normal;text-decoration:none}
.pagenavoff:hover{color:#0000ff;font-weight:normal;text-decoration: none;background-color:#7099cc}
.pagenavoff:active{color:#;font-weight:normal;text-decoration:none}
.pagenavon {text-align: center;border: 1px #f5f5f5 solid ;padding: 5px;padding-left: 12px;padding-right: 12px}
原文地址:https://www.cnblogs.com/code123/p/3356885.html