2014-03-28 64 views
2

我有181个产品的数据库。 我使用PHP来在页面上显示产品。 其显示每页20个产品,总共有10页,最后一页,第10页只是一个产品。 对于181种产品,分页从第0页开始,如< 0 1 2 3..10 >。 对于像180这样的舍入值,分页显示正确< 1 2 3..10 >分页从0开始

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

我的猜测变化是它在最后其他。你需要做一些调试代码,看看'$ this-> getPages()'的值是多少。 – teynon

+0

如果你有'$ this-> getPages()'= 10那么它将从0开始(10-10) – ciprian2301

回答

1

在拉斯维加斯要不然这

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

正在工作。 – speedy