//用法1:
echo Page::instance(32,10,5,5,'show?p=','&lang=zh')->show();
//用法2:
echo Page::instance()->show(32,10,5,5,'show?p=','&lang=zh');
//用法3:
$page = Page::instance();
$page->set_config('theme', ' %first% %up% %prev% %page% %next% %down% %last% ');
echo $page->show(32,10,5,5,'show?p=','&lang=zh');
//用法4:
$page = Page::instance(32,10,5,5,'show?p=','&lang=zh');
echo $page->get('page_theme');
echo $page->get('next');//获取下一页,这里显示有设置的主题
//用法5:
$page = Page::instance();
$page->set(32,10,5,5,'show?p=','&lang=zh');
echo $page->get('page_theme');
echo $page->get('next');//获取下一页,这里显示有设置的主题
//注:该类不能对已设数据进行覆盖,比如32,10这些在同一次的执行中不能被其他数据覆盖。如果要进行覆盖,可将已设数据更改为NULL
<?php
/*
*
* @ID: 分页类
*
* @auther: 欣儿
*
* @time: 2014/02/20
*
* @web: http://my.oschina.net/xinger
*
**/
class Page {
public $left = '%';
public $right = '%';
public $first = 1 ;
public $count;
public $perlogs;
public $page;
public $roll_page;
public $url;
public $suffix;
public $total;//总页数
public $prev;//上一页
public $next;//下一页
public $up;//上大翻页
public $down;//下大翻页
protected $now_page;
protected $other_page;
protected $prev_page;
protected $next_page;
protected $up_page;
protected $down_page;
protected $first_page;
protected $last_page;
protected $var = array();//暂存数据
protected $config = array(
'other_theme'=>'<span class="current"><a href="%page_url%">%i%</a></span>',//页面页
'now_theme'=>'<span class="current_show"><a href="%now_url%">%i%</a></span>',//当前页 %page%为other和now的组合
'prev_theme'=>'<span class="current_pre"><a href="%prev_url%" title="上一页"><上一页</a></span>',//url可在theme单独使用
'next_theme'=>'<span class="current_next"><a href="%next_url%" title="下一页">下一页></a></span>',
'up_theme'=>'<span class="current_up"><a href="%up_url%" title="<<"><<</a></span>',//
'down_theme'=>'<span class="current_down"><a href="%down_url%" title=">>">>></a></span>',
'first_theme'=>'<span class="current_start"><a href="%first_url%" title="首页">首页</a></span>',
'last_theme'=>'<span class="current_end"><a href="%last_url%" title="尾页">尾页</a></span>',
'prev_end_theme'=>'',//不显示的时候总是显示时显示的主题
'next_end_theme'=>'',
'up_end_theme'=>'',//
'down_end_theme'=>'',
'first_end_theme'=>'',
'last_end_theme'=>'',
'theme'=>' %first% %up% %prev% %page% %next% %down% %last% ',//显示样式 共有 %count% 条数据, 第 %now_page% / %total% 页 %prev_always% %up_always% %down_always%
);
public function __construct($count = NULL, $perlogs = NULL, $now_page = NULL, $roll_page = NULL, $url = NULL, $suffix = NULL) {
$this->set($count, $perlogs, $now_page, $roll_page, $url, $suffix);
}
public static function instance($count = NULL, $perlogs = NULL, $now_page = NULL, $roll_page = NULL, $url = NULL, $suffix = NULL) {
static $var;
if(isset($var)) return $var;
$var = new self($count, $perlogs, $now_page, $roll_page, $url, $suffix);
return $var;
}
//显示
public function show($count = NULL, $perlogs = NULL, $now_page = NULL, $roll_page = NULL, $url = NULL, $suffix = NULL) {
$this->set($count, $perlogs, $now_page, $roll_page, $url, $suffix);
$this->compile();
$page = $this->var['page_theme'];
return $page;
}
//获取返回信息
public function get($key = NULL) {
$this->compile();
if(NULL !== $key && isset($this->var[$key])) return $this->var[$key];
return $this->var;
}
//设置: 条目总数, 每页显示条数目, 当前页码, 当前页码的前后页数, 页码的地址, 地址其他项
public function set($count = NULL, $perlogs = NULL, $now_page = NULL, $roll_page = NULL, $url = NULL, $suffix = NULL) {
if (NULL === $this->count) $this->count = $this->var['count'] = $count;
if (NULL === $this->perlogs) $this->perlogs = $this->var['perlogs'] = $perlogs;
if (NULL === $this->page) $this->page = $this->var['now_page'] = $now_page;
if (NULL === $this->roll_page) $this->roll_page = $this->var['roll_page'] = $roll_page;//与当前页左右相距的页数
if (NULL === $this->url) $this->url = $this->var['url'] = $url;
if (NULL === $this->suffix) $this->suffix = $this->var['suffix'] = $suffix;
}
public function set_config($name, $value = NULL) {
if (isset($this->config[$name])) {
$this->config[$name] = $value;
return true;
}
return false;
}
public function get_config($name = NULL) {
if (NULL === $name) return $this->config;
if (isset($this->config[$name])) {
return $this->config[$name];
}
return false;
}
public function get_count() {
return $this->count;
}
public function get_perlogs() {
return $this->perlogs;
}
public function get_page() {
return $this->page;
}
public function get_roll_page() {
return $this->roll_page;
}
public function get_url() {
return $this->url;
}
public function get_suffix() {
return $this->suffix;
}
public function get_first() {
return $this->first;
}
protected function get_total() {
$this->total = @ceil($this->count / $this->perlogs);
return $this->total;
}
protected function get_prev() {
$this->prev = $this->page - 1;
return $this->prev;
}
protected function get_next() {
$this->next = $this->page + 1;
return $this->next;
}
protected function get_up($i = 5) {
$this->up = $this->page - $i;
return $this->up;
}
protected function get_down($i = 5) {
$this->down = $this->page + $i;
return $this->down;
}
protected function get_prev_page($always = false) {
if ($always) $this->prev += 1;
$this->prev_page = $this->url . $this->prev . $this->suffix;
return $this->prev_page;
}
protected function get_next_page($always = false) {
if ($always) $this->next -= 1;
$this->next_page = $this->url . $this->next . $this->suffix;
return $this->next_page;
}
protected function get_up_page($always = false) {
$roll_page = $this->get_roll_page();
$now_page = $this->get_page();
$first = $this->get_first();
if ($always) {
if ($now_page <= $roll_page * 2 && $now_page > $roll_page) {
$this->up += $roll_page;
} else {
$this->up = $first;
}
}
$this->up_page = $this->url . $this->up . $this->suffix;
return $this->up_page;
}
protected function get_down_page($always = false) {
$total = $this->get_total();
$roll_page = $this->get_roll_page();
$now_page = $this->get_page();
if ($always) {
$page = $total - $now_page;
if ($page <= $roll_page * 2 && $page >= $roll_page) {
$this->down -= $roll_page;
} else {
$this->down = $total;
}
}
$this->down_page = $this->url . $this->down . $this->suffix;
return $this->down_page;
}
protected function get_first_page() {
$this->first_page = $this->url . $this->first . $this->suffix;
return $this->first_page;
}
protected function get_last_page() {
$this->last_page = $this->url . $this->last . $this->suffix;
return $this->last_page;
}
protected function get_now_page($i = NULL) {
$this->now_page = $this->url . $i . $this->suffix;
return $this->now_page;
}
protected function get_other_page($i = NULL) {
$this->other_page = $this->url . $i . $this->suffix;
return $this->other_page;
}
protected function get_prev_theme($always = false) {
$total = $this->get_total();
$now_page = $this->get_page();
$prev = $this->get_prev();
$prev_page = ($always === true) ? $this->get_prev_page(true) : $this->get_prev_page();
$prev_always = NULL;
if($prev > 0 && $now_page <= $total) {
$prev_always = strtr($this->config['prev_theme'], array($this->left . 'prev_url' . $this->right => $prev_page));
}
if($prev <= 0 && $always) {
if(!empty($this->config['prev_end_theme'])) {
$prev_theme = $this->config['prev_end_theme'];
} else {
$prev_theme = $this->config['prev_theme'];
}
$prev_always = strtr($prev_theme, array($this->left . 'prev_url' . $this->right => $prev_page));
}
return $prev_always;
}
protected function get_next_theme($always = false) {
$next = $this->get_next();
$total = $this->get_total();
$next_page = ($always === true) ? $this->get_next_page(true) : $this->get_next_page();
$next_always = NULL;
if($next < $total + 1){
$next_always = strtr($this->config['next_theme'], array($this->left . 'next_url' . $this->right => $next_page));
}
if($next >= $total + 1 && $always){
if(!empty($this->config['next_end_theme'])) {
$next_theme = $this->config['next_end_theme'];
} else {
$next_theme = $this->config['next_theme'];
}
$next_always = strtr($next_theme, array($this->left . 'next_url' . $this->right => $next_page));
}
return $next_always;
}
protected function get_up_theme($always = false) {
$total = $this->get_total();
$roll_page = $this->get_roll_page();
$now_page = $this->get_page();
$roll_page = $roll_page * 2;
$up = $this->get_up($roll_page);
$up_page = ($always === true) ? $this->get_up_page(true) : $this->get_up_page();
$up_always = NULL;
if($up > 0 && $now_page <= $total){
$up_always = strtr($this->config['up_theme'], array($this->left . 'up_url' . $this->right => $up_page));
}
if($up <= 0 && $always){
if(!empty($this->config['up_end_theme'])){
$up_theme = $this->config['up_end_theme'];
} else {
$up_theme = $this->config['up_theme'];
}
$up_always = strtr($up_theme, array($this->left . 'up_url' . $this->right => $up_page));
}
return $up_always;
}
protected function get_down_theme($always = false) {
$total = $this->get_total();
$roll_page = $this->get_roll_page();
$roll_page = $roll_page * 2;
$down = $this->get_down($roll_page);
$down_page = ($always === true) ? $this->get_down_page(true) : $this->get_down_page();
$down_always = NULL;
if($down < $total + 1){
$down_always = strtr($this->config['down_theme'], array($this->left . 'down_url' . $this->right => $down_page));
}
if($down >= $total + 1 && $always){
if(!empty($this->config['down_end_theme'])){
$down_theme = $this->config['down_end_theme'];
} else {
$down_theme = $this->config['down_theme'];
}
$down_always = strtr($down_theme, array($this->left . 'down_url' . $this->right => $down_page));
}
return $down_always;
}
protected function get_page_theme($always = false) {
$total = $this->get_total();
$now_page = $this->get_page();
$roll_page = $this->get_roll_page();
$page = NULL;
for ($i = $now_page - $roll_page;$i <= $now_page + $roll_page && $i <= $total; $i++){
if ($i > 0){
if ($i == $now_page) {
$page .= strtr($this->config['now_theme'], array($this->left . 'page_url' . $this->right => $this->get_now_page($i),$this->left . 'now_url' . $this->right => $this->get_other_page($i),$this->left . 'i' . $this->right => $i));
} else {
$page .= strtr($this->config['other_theme'], array($this->left . 'page_url' . $this->right => $this->get_now_page($i),$this->left . 'now_url' . $this->right => $this->get_other_page($i),$this->left . 'i' . $this->right => $i));
}
}
}
if ($total <= 1 && $always == false) $page = '';
return $page;
}
protected function get_first_theme($always = false) {
$total = $this->get_total();
$now_page = $this->get_page();
$roll_page = $this->get_roll_page();
$first_page = $this->get_first_page();
$first_always = NULL;
if ($now_page > $roll_page + 1 && $now_page <= $total) {
$first_always = strtr($this->config['first_theme'], array($this->left . 'first_url' . $this->right => $first_page));
}
if ($now_page <= $roll_page + 1 && $always) {
if(!empty($this->config['first_end_theme'])) {
$first_theme = $this->config['first_end_theme'];
} else {
$first_theme = $this->config['first_theme'];
}
$first_always = strtr($first_theme, array($this->left . 'first_url' . $this->right => $first_page));
}
return $first_always;
}
protected function get_last_theme($always = false) {
$now_page = $this->get_page();
$total = $this->get_total();
$roll_page = $this->get_roll_page();
$last_page = $this->get_last_page();
$last_always = NULL;
if ($now_page + $roll_page < $total || $always) {
$last_always = strtr($this->config['last_theme'], array($this->left . 'last_url' . $this->right => $last_page));
}
if ($now_page + $roll_page >= $total && $always) {
if(!empty($this->config['last_end_theme'])){
$last_theme = $this->config['last_end_theme'];
} else {
$last_theme = $this->config['last_theme'];
}
$last_always = strtr($last_theme, array($this->left . 'last_url' . $this->right => $last_page));
}
return $last_always;
}
//compile
protected function compile() {
$count = $this->get_count();
$now_page = $this->get_page();
$roll_page = $this->get_roll_page();
$total = $this->var['total'] = $this->last = $this->get_total();
$prev_url = $this->var['prev_url'] = $this->get_prev_page();
$next_url = $this->var['next_url'] = $this->get_next_page();
$first_url = $this->var['first_url'] = $this->get_first_page();
$last_url = $this->var['last_url'] = $this->get_last_page();
$prev = $this->var['prev'] = $this->get_prev_theme();
$next = $this->var['next'] = $this->get_next_theme();
$page = $this->var['page'] = $this->get_page_theme();
$up = $this->var['up'] = $this->get_up_theme();
$down = $this->var['down'] = $this->get_down_theme();
$first = $this->var['first'] = $this->get_first_theme();
$last = $this->var['last'] = $this->get_last_theme();
$prev_always = $this->var['prev_always'] = $this->get_prev_theme(true);
$next_always = $this->var['next_always'] = $this->get_next_theme(true);
$page_always = $this->var['page_always'] = $this->get_page_theme(true);
$up_always = $this->var['up_always'] = $this->get_up_theme(true);
$down_always = $this->var['down_always'] = $this->get_down_theme(true);
$first_always = $this->var['first_always'] = $this->get_first_theme(true);
$last_always = $this->var['last_always'] = $this->get_last_theme(true);
$theme_parse = array(
$this->left . 'first' . $this->right => $first,
$this->left . 'up' . $this->right => $up,
$this->left . 'prev' . $this->right => $prev,
$this->left . 'page' . $this->right => $page,
$this->left . 'next' . $this->right => $next,
$this->left . 'down' . $this->right => $down,
$this->left . 'last' . $this->right => $last,
$this->left . 'first_always' . $this->right => $first_always,
$this->left . 'up_always' . $this->right => $up_always,
$this->left . 'prev_always' . $this->right => $prev_always,
$this->left . 'page_always' . $this->right => $page_always,
$this->left . 'next_always' . $this->right => $next_always,
$this->left . 'down_always' . $this->right => $down_always,
$this->left . 'last_always' . $this->right => $last_always,
$this->left . 'total' . $this->right => $total,
$this->left . 'count' . $this->right => $count,
$this->left . 'now_page' . $this->right => $now_page,
$this->left . 'roll_page' . $this->right => $roll_page,
$this->left . 'prev_url' . $this->right => $prev_url,
$this->left . 'next_url' . $this->right => $next_url,
$this->left . 'first_url' . $this->right => $first_url,
$this->left . 'last_url' . $this->right => $last_url,
);
$page_theme = $this->var['page_theme'] = strtr($this->config['theme'], $theme_parse);
return $page_theme;
}
}
?>
PHP 分页类
点赞
收藏