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()."'>«</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()."'>»</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()."'>«</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()."'>»</a>\n ";
}
}
return $trail;
}
我的猜測變化是它在最後其他。你需要做一些調試代碼,看看'$ this-> getPages()'的值是多少。 – teynon
如果你有'$ this-> getPages()'= 10那麼它將從0開始(10-10) – ciprian2301