我正在对mysql结果进行一些分页,并试图限制列出到指定变量的给定页面的数量。它部分工作,但限制仅适用于开始和结束。我相信这是因为我需要修改for
循环。
以下是目前显示的内容。
^应该只显示3,4,5,6,7 ^
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>' . ' ';
}
}
答案 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>' . ' ';
}
}