我有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;
}

最佳答案

其他地方的变化

        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 ";
            }

关于php - 分页从0开始,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22724602/

10-16 08:19