2
votes

I have database with 181 products. I'm using php to display the products on pages . Its displaying 20 products per page, in total I have 10 pages and on the last page, page 10 just one product. For 181 products the pagination starts from page 0 like < 0 1 2 3..10 >. For rounded values like 180 the pagination is displayed correctly < 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;
}

And a function to display the page numbers

    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
My guess would be its in the final else. You need to do throw some debug code and see what the value of $this->getPages() is. - teynon
If you have $this->getPages() = 10 then it will start from 0 (10-10) - ciprian2301

1 Answers

1
votes

Change in the 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 ";
        }

with

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