简体   繁体   中英

Pagination issue with multiple parameters

I have a pagination class that works grate for urls like

http://localmachine/c/category-name

The pagination links will look like

http://localmachine/c/category-name/page-2 

But if I add another parameter to base url like:

http://localmachine/c/category-name/brand-Coca+Cola 

the links for pagination will be

http://localmachine/brand/Coca+Cola/c/category-name/page-2

How can I fix this to display the pagination link like

 http://localmachine/c/category-name/brand/Coca+Cola/page-2

This is my pagination function that displays the pagination links.

function buildTrail($param = ""){


    if(is_array($param)){
        foreach($param as $a => $b){
            if($a != "page"){
                $url .= $a . "/" . urlencode($b).'/';
            }else{
                $url .= '';
            }
        }
    } else {
        $url = $param;
    }

    //$location = basename($_SERVER['REQUEST_URI']);



    $trail = "";
    if($this->getPages() > 1){
        if($this->getFrom() > 1){
        $trail .= "<a href='" . WEBSITE . "/". $url . "page-" . $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 . "page-" . $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 . "page-" . $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 . "page-" . $i . "'>" . $i . "</a>\n ";
            }
        } else {            
            for ($i = ($this->getPages() - 9); $i <= $this->getPages(); $i ++){
                $trail .= "<a  class='". ($i == $this->getFrom() ? "selected" : "links") . "' href='" . WEBSITE . "/". $url . "page-" . $i .  "'>" . $i . "</a>\n ";
            }
        }
        if($this->getFrom() < $this->getPages()){
        $trail .= "<a href='" . WEBSITE . "/". $url . "page-" . $this->getNext() . "'>&raquo;</a>\n ";
        }
    }

    return $trail;
}

I'd do it like this:

    // Create array with preferred order of parameters first.
    // Make sure to list all possible params here excluding page
    $preferedOrder = array('c', 'brand');

    $paramsArray = array();

    foreach ($preferedOrder as $v) {

        // Based on the type of parameter combine it with / or - . Put everything to array.
        if (isset($param[$v])) {
            if ($v == 'c') {
                $paramsArray[] = $v . '/' . urlencode($param[$v]);
            } else {
                $paramsArray[] = $v . '-' . urlencode($param[$v]);
            }
        }
    }

    // Implode array with '/'
    $url = implode('/', $paramsArray);

In this case all parameters will be in the required order and page will not be there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM