【问题标题】:Pagination starts from 0分页从 0 开始
【发布时间】:2014-04-29 04:18:31
【问题描述】:

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

【问题讨论】:

  • 我的猜测是它在决赛中。你需要抛出一些调试代码,看看$this-&gt;getPages() 的值是多少。
  • 如果你有 $this-&gt;getPages() = 10 那么它将从 0 (10-10) 开始

标签: php mysql pagination


【解决方案1】:

改变las else this

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-14
    • 2019-02-07
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    相关资源
    最近更新 更多