在分页中仅显示特定范围的页面?

时间:2014-12-18 23:38:39

标签: php pagination

我正在对mysql结果进行一些分页,并试图限制列出到指定变量的给定页面的数量。它部分工作,但限制仅适用于开始和结束。我相信这是因为我需要修改for循环。

以下是目前显示的内容。

enter image description here

enter image description here
^应该只显示3,4,5,6,7 ^

enter image description here

PHP:

var $max_pages = 4;

$batch = ceil($this->page / $this->links_per_page );
$end = $batch * $this->links_per_page;
if ($end > $this->total_pages) {
    $end = $this->total_pages;
}

$start = 1;

for($i = max($start,$this->page - $this->max_pages);
$i <= min($this->page + $this->max_pages,$end); $i ++) {
    if ($i == $this->page) {
        $prefixActive = '<span class="page_link active">';
        $links .= $prefixActive . "$i" . $suffix;
    } else {
         $links .= ' ' . '<a href="' . $this->php_self . 
         '?page=' . $i . $this->append . '">' .$prefix . $i .
         $suffix. '</a>' . ' ';
    }
}

1 个答案:

答案 0 :(得分:0)

if($this->total_pages > $this->max_pages){ 
    $max_start = max($start,$this->page - $this->max_pages / 2); 
    $start_index = min($this->total_pages - $this->max_pages, $max_start); 
    $end_index = min($this->total_pages, $max_start + $this->max_pages); 
} else { 
    $start_index = 1; 
    $end_index = $this->total_pages; 
} 

for($i = $start_index; $i <= $end_index; $i ++) { 
   if ($i == $this->page) { 
       $prefixActive = '<span class="page_link active">'; 
       $links .= $prefixActive . "$i" . $suffix; 
   } else { 
       $links .= ' ' . '<a href="' . $this->php_self . 
               '?page=' . $i . $this->append . '">' .$prefix . $i . 
               $suffix. '</a>' . ' '; 
   } 
}