分页从0开始

时间:2014-03-28 23:00:17

标签: php mysql pagination

我有181个产品的数据库。 我使用php在页面上显示产品。 它每页显示20个产品,总共有10页,在最后一页,第10页只有一个产品。 对于181种产品,分页从第0页开始,如< 0 1 2 3..10 >。 对于像180这样的舍入值,分页显示正确< 1 2 3..10 >

    function buildTrail($param = ""){

    $cur_page = basename($_SERVER['PHP_SELF']);
    $link = $_SERVER['REQUEST_URI'];
    $link_array = explode('/', $link);
    //$count = count($link_array);
    $pagename = $link_array[1];
    // echo $magename;
    //echo $link;

    if(is_array($param)){
        foreach($param as $a => $b){
            if($a != "page" && $a != "b" && $a != "q" && $a != "oferta"){
                $url = "/".$b."/pg/";
            }elseif($a == "b"){
                $url = "/".$pagename."/brand/".$b."/pg/";
            }elseif($a == "oferta"){
                $url = "/".$pagename."/".$a."/pg/";
            }else{
                $url = "/".$pagename."/".$b."/pg/";
            }
        }
    }else {
        $url = $param;
    }   
    // print_r($b);


    $trail = "";
    if($this->getPages() > 1){
        if($this->getFrom() > 1){
        $trail .= "<a href='" . WEBSITE . $url . $this->getPrevious()."'>&laquo;</a>\n ";
        }

        if($this->getFrom() < 10 && $this->getPages() > 10){
            for ($i = 1; $i <= 10; $i++){
                $trail .= "<a class='". ($i == $this->getFrom() ? "selected" : "links") . "' href='". WEBSITE .$url. $i ."'>" . $i . "</a>\n ";
            }
        } elseif($this->getFrom() < 10 && $this->getPages() < 10){
            for ($i = 1; $i <= $this->getPages(); $i++){
                $trail .= "<a class='". ($i == $this->getFrom() ? "selected" : "links") . "' href='". WEBSITE .$url. $i ."'>" . $i . "</a>\n ";
            }
        }elseif ($this->getFrom() >= 10 && $this->getFrom() <= ($this->getPages() - 5) ){
            for ($i = ($this->getFrom() - 5); $i <= ($this->getFrom() + 5); $i ++){
                $trail .= "<a  class='". ($i == $this->getFrom() ? "selected" : "links") . "' href='" . WEBSITE . $url. $i ."'>" . $i . "</a>\n ";
            }
        } else {            
            for ($i = ($this->getPages() - 10); $i <= $this->getPages(); $i ++){
                $trail .= "<a  class='". ($i == $this->getFrom() ? "selected" : "links") . "' href='" . WEBSITE . $url. $i ."'>" . $i . "</a>\n ";
            }
        }
        if($this->getFrom() < $this->getPages()){
        $trail .= "<a href='" . WEBSITE .$url. $this->getNext()."'>&raquo;</a>\n ";
        }
    }

    return $trail;
}

显示页码的功能

    function buildTrail($param = ""){

    $cur_page = basename($_SERVER['PHP_SELF']);
    $link = $_SERVER['REQUEST_URI'];
    $link_array = explode('/', $link);
    //$count = count($link_array);
    $pagename = $link_array[1];
    // echo $magename;
    //echo $link;

    if(is_array($param)){
        foreach($param as $a => $b){
            if($a != "page" && $a != "b" && $a != "q" && $a != "oferta"){
                $url = "/".$b."/pg/";
            }elseif($a == "b"){
                $url = "/".$pagename."/brand/".$b."/pg/";
            }elseif($a == "oferta"){
                $url = "/".$pagename."/".$a."/pg/";
            }else{
                $url = "/".$pagename."/".$b."/pg/";
            }
        }
    }else {
        $url = $param;
    }   
    // print_r($b);


    $trail = "";
    if($this->getPages() > 1){
        if($this->getFrom() > 1){
        $trail .= "<a href='" . WEBSITE . $url . $this->getPrevious()."'>&laquo;</a>\n ";
        }

        if($this->getFrom() < 10 && $this->getPages() > 10){
            for ($i = 1; $i <= 10; $i++){
                $trail .= "<a class='". ($i == $this->getFrom() ? "selected" : "links") . "' href='". WEBSITE .$url. $i ."'>" . $i . "</a>\n ";
            }
        } elseif($this->getFrom() < 10 && $this->getPages() < 10){
            for ($i = 1; $i <= $this->getPages(); $i++){
                $trail .= "<a class='". ($i == $this->getFrom() ? "selected" : "links") . "' href='". WEBSITE .$url. $i ."'>" . $i . "</a>\n ";
            }
        }elseif ($this->getFrom() >= 10 && $this->getFrom() <= ($this->getPages() - 5) ){
            for ($i = ($this->getFrom() - 5); $i <= ($this->getFrom() + 5); $i ++){
                $trail .= "<a  class='". ($i == $this->getFrom() ? "selected" : "links") . "' href='" . WEBSITE . $url. $i ."'>" . $i . "</a>\n ";
            }
        } else {            
            for ($i = ($this->getPages() - 10); $i <= $this->getPages(); $i ++){
                $trail .= "<a  class='". ($i == $this->getFrom() ? "selected" : "links") . "' href='" . WEBSITE . $url. $i ."'>" . $i . "</a>\n ";
            }
        }
        if($this->getFrom() < $this->getPages()){
        $trail .= "<a href='" . WEBSITE .$url. $this->getNext()."'>&raquo;</a>\n ";
        }
    }

    return $trail;
}

1 个答案:

答案 0 :(得分:1)

改变其他方式

        for ($i = ($this->getPages() - 10); $i <= $this->getPages(); $i ++){
            $trail .= "<a  class='". ($i == $this->getFrom() ? "selected" : "links") . "' href='" . WEBSITE . $url. $i ."'>" . $i . "</a>\n ";
        }

            for ($i = 1; $i <= $this->getPages(); $i ++){
                $trail .= "<a  class='". ($i == $this->getFrom() ? "selected" : "links") . "' href='" . WEBSITE . $url. $i ."'>" . $i . "</a>\n ";
            }