问题
I have implemented a Zend Pagination but can't figure out how to limit the number of links in the pagination. I know about setPageRange, but it isn't exactly what i want.
Currently the pagination looks like this.
< | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | >
But what i want is something like this:
for page 1
< | 1 | 2 | 3 | ... | 14 | 15 | 16 | >
for page 8
< | 1 | 2 | 3 | ... | 7 | 8 | 9 | ... | 14 | 15 | 16 | >
for page 14
< | 1 | 2 | 3 | ... | 13 | 14 | 15 | 16 | >
回答1:
here i can give you pagination helper script which i have implemented in one of my application and it is same like as above you want.
<?php
if ($this->pageCount) :
$midRange = floor(sizeof($this->pagesInRange) / 2);
if (($this->current + $midRange) < $this->pageCount && $this->pageCount > 5) :
array_pop($this->pagesInRange);
$display_end = true;
endif;
?>
<div class="paginationControl<?php echo $this->position; ?>">
<div class="paginationControl_pages">
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<?php if($this->extraVar != "" ){ ?>
<a href="<?php echo $this->url(array_merge($this->extraVar,array('page' => $this->previous))). $this->query; ?>" class="paging-active"> Previous </a>
<?php }else {?>
<a href="<?php echo $this->url(array('page' => $this->previous)). $this->query; ?>" class="paging-active"> Previous </a>
<?php }?>
<?php else: ?>
<span class="paging-active"><strong> Previous </strong></span>
<?php endif; ?>
<span ><?php if (($this->current - $midRange) > $this->first && $this->pageCount > 5): ?></span>
<?php
array_shift($this->pagesInRange);?>
<?php if($this->extraVar != "" ){ ?>
<a href="<?php echo $this->url(array_merge($this->extraVar,array('page' => $this->first))) . $this->query; ?>" ><span class="paging"><?php echo $this->first ?></span></a>...
<?php } else {?>
<a href="<?php echo $this->url(array('page' => $this->first)) . $this->query; ?>" ><span class="paging"><?php echo $this->first ?></span></a>...
<?php }?>
<?php endif; ?>
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<?php if($this->extraVar != "" ){ ?>
<a href="<?php echo $this->url(array_merge($this->extraVar,array('page'=>$page))); ?>""><span class="paging"><?php echo $page; ?></span></a>
<?php }else{ ?>
<a href="<?php echo $this->url(array('page'=>$page)); ?>"><span class="paging"><?php echo $page; ?></span></a>
<?php } ?>
<?php else: ?>
<span class="paging-active"><strong><?php echo $page; ?> </strong></span>
<?php endif; ?>
<?php endforeach; ?>
<?php if (!empty($display_end)) :
?>
<?php if($this->extraVar != "" ){ ?>
...<a href="<?php echo $this->url(array_merge($this->extraVar,array('page' => $this->last))) . $this->query; ?>" ><span class="paging"><?php echo $this->last ?></span></a>
<?php }else{?>
...<a href="<?php echo $this->url(array('page' => $this->last)) . $this->query; ?>" ><span class="paging"><?php echo $this->last ?></span></a>
<?php }?>
<?php endif; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
<?php if($this->extraVar != "" ){ ?>
<a href="<?php echo $this->url(array_merge($this->extraVar,array('page' => $this->next))) . $this->query; ?>" class="paging-active"> Next </a>
<?php }else{?>
<a href="<?php echo $this->url(array('page' => $this->next)) . $this->query; ?>" class="paging-active"> Next </a>
<?php }?>
<?php else: ?>
<span class="paging-active"><strong>Next </strong></span>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
hope this will sure help you to get your desire OUTPUT.
回答2:
Using this will produce the desired output.
For example with last number as 20, first number as 1, current with 8, and limit with 3 links will produce output
first,1,..,7,8,9,..,20,last
<?php
$last=20;
$current=8;
$first=1;
$output="";
$show_limit=3;
if($show_limit==$last){
for($i=1;$i<=$last;$i++){
if(empty($output))
$output.="$i";
else
$output.=",$i";
}
$output="first,$output,last";
}else{
if($current-ceil($show_limit/2) <=$first){
for($i=1;$i<=$show_limit+1;$i++){
$output.=",$i";
}
$output="first$output,..,$last,last";
}else if($current+ceil($show_limit/2)>=$last){
for($i=1;$i<$show_limit+1;$i++){
$output=",". intval($last-$i). $output;
}
$output="first,1,..".$output. ",$last,last";
}else{
$output="first,1,..,";
$start=$current-floor($show_limit/2);
for($i=0;$i<$show_limit;$i++){
$cursor=intval($start+$i);
if($cursor==$last)
break;
$output.=$cursor.",";
}
$output.="..,$last,last";
}
}
echo $output."\n";
来源:https://stackoverflow.com/questions/19270804/zend-pagination-with-limited-number-of-links-and-dots-between