无论写了多少遍,无论编程的技术到了何种的境界,还是会涉及到一种需求:分页。
很多的分页代码都会有很多的判断,是不是第一页,是不是最后一页等,今天在惠新宸的博客上看见一种巧妙的算法,通过简单的比较代替了很多的判断,试了很久,很难写出比这个更简单的代码了。下面是自己研究后自己重新敲了一遍的代码:
class Pagination
{
private $page,$page_size,$total,$total_page,$size;
private $prev_str = '<',$next_str = '>';
private $class = 'page',$prev_class = 'prev',$next_class = 'next',$selected_class = 'selected',
$no_prev_class = 'no-prev',$no_next_class = 'no-next';
private $para_name = 'page',$url_prefix = '',$target = '';
private $page_str = array();
function __construct($url_prefix,$page_size,$total,$size=10,$conf=array())
{
$this->url_prefix = $url_prefix;
$this->page_size = $page_size;
$this->total = $total;
$this->total_page = intval(ceil($total/$page_size));
$this->size = $size;
if( ! empty($conf)){
$configure = array('prev_str','next_str','class','selected_class');
foreach($conf as $key => $val){
if(in_array($val,$configure)){
$this->$key = $val;
}
}
}
if(strstr($url_prefix,'?') === false){
$this->url_prefix .= '?' . $this->para_name . '=';
}else{
$this->url_prefix .= '&' . $this->para_name . '=';
}
}
function generate($page){
$this->page = $page;
if(isset($this->page_str[$page])){
return $this->page_str[$page];
}
$half = intval($this->size/2);
$page_start = max(1,$this->page - $half);
$page_end = min($page_start + $this->size - 1,$this->total_page);
$page_start = max(1, $page_end - $this->size + 1);
$this->page_str[$page] = $this->build_nav_str($page_start,$page_end);
return $this->page_str[$page];
}
function build_nav_str($page_start,$page_end){
$page_nums = range($page_start,$page_end);
if($this->page == 1){
$page_str = <<<HTML
<span class="{$this->no_prev_class}">{$this->prev_str}</span>
HTML;
}else{
$page = $this->page - 1;
$page_str = <<<HTML
<span class="{$this->prev_class}"><a href="{$this->url_prefix}{$page}" {$this->target}>{$this->prev_str}</a></span>
HTML;
}
foreach($page_nums as $p){
$page_str .= ($p == $this->page) ? <<<HTML
<span class="{$this->selected_class}">
{$p}
</span>
HTML
:<<<HTML
<span class="{$this->class}">
<a href="{$this->url_prefix}{$p}" {$this->target}>{$p}</a>
</span>
HTML;
}
if($this->page == $this->total_page){
$page_str .= <<<HTML
<span class="{$this->no_next_class}">{$this->next_str}</span>
HTML;
}else{
$page = $this->page + 1;
$page_str .= <<<HTML
<span class="{$this->next_class}">
<a href="{$this->url_prefix}{$page}" {$this->target}>{$this->next_str}</a>
</span>
HTML;
}
return $page_str;
}
}
里面有几个关键的地方:
- generate方法里的起始页和结束页计算方法。
- page和generate方法分开,批量生成分页代码的时候节省内存。
- nowdoc的使用,使得html代码可以着色,容易识别。
网友评论