2014-08-28 42 views
1

我有一個分頁類,對於網址工作篦狀分頁問題有多個參數

http://localmachine/c/category-name 

分頁鏈接看起來像

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

但是,如果我添加其他參數基本URL,如:

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

了分頁鏈接將

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

我怎樣才能解決這個問題,顯示一個類似

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

這是一個顯示分頁鏈接我的分頁功能,分頁鏈接。

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; 
} 
+0

檢查你在$ _GET varialbe中獲得的參數 – Swapnil 2014-08-28 10:49:38

回答

1

我會做這樣的:

// 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); 

在這種情況下,所有的參數將在需要的順序和頁面不會在那裏。

+0

對於第一個元素'c',我需要用'/'ex:c/categoryname /和下一個元素用'/ param-value /' 。 – speedy 2014-08-28 12:05:21

+0

第一個元素'c'需要與'/'不匹配' - ' – speedy 2014-08-28 12:21:44

+1

查看最新版本 – Stepashka 2014-08-28 12:23:44