基础区分页类、函数汇总展示
基础区分页类、函数汇总展示<?php
/*
PHP 5 分页类 | 每次输出10个链接
作者:Mosee
调用示例:
$per = 8; //每页输出8条数据
$page = @$_GET['page'];
$page = ((int)$page > 0) ? ((int)$page) : 1;
$query = "SELECT id FROM content";
$result = mysql_query($query);
$total = mysql_num_rows($result);
if ($total < 1) $total = 1;
if ($page > ceil($total / $per)) $page = ceil($total / $per);
$query = "SELECT * FROM content LIMIT ".(($page - 1) * $per).",$per";
$result = mysql_query($query);
// 输出略……
$query_page = 'msg.php?act=show&box=1&';
require(JPA_ROOT.'include/page.class.php');
$pages = new page($total, $per, $query_page);
echo $pages->show();
输出HTML示例:
<ul class="paginator">
<li><a href="msg.php?act=show&box=1&page=1">首页</a></li>
<li><a href="msg.php?act=show&box=1&page=2">上一页</a></li>
<li><a href="msg.php?act=show&box=1&page=1">1</a></li>
<li><a href="msg.php?act=show&box=1&page=2">2</a></li>
<li class="current"><a>3</a></li>
</ul>
*/
class page {
private $pagesnum; //总页数
private $now_page; //当前页
private $head_page; //第一个分页链接
private $foot_page; //最后一个分页链接
private $query_page;
private $total; //总数据数
private $per; //每页输出的数据数
private $str; //页面链接字符串
private $result; //结果
public function __construct($total, $per, $query_page) {
$this->total = $total;
$this->per = $per;
$this->query_page = $query_page;
$this->get_now_page();
$this->get_head_foot_page();
}
//获得当前页面
private function get_now_page() {
$this->now_page = isset($_GET['page'])?intval($_GET['page']):1;
$this->pagesnum = ceil($this->total/$this->per);
if($this->now_page > $this->pagesnum) {
$this->now_page = $this->pagesnum;
}
if($this->now_page < 1) {
$this->now_page = 1;
}
}
//获取第一个链接和最后一个链接
private function get_head_foot_page() {
$this->head_page = $this->now_page - 4;
$this->foot_page = $this->now_page + 5;
if($this->head_page < 1) {
$this->head_page = 1;
if($this->pagesnum < 10) {
$this->foot_page = $this->pagesnum;
} else {
$this->foot_page = 10;
}
}
if($this->foot_page > $this->pagesnum) {
$this->foot_page = $this->pagesnum;
$this->head_page = $this->pagesnum - 9;
if ($this->head_page < 1) {
$this->head_page = 1;
}
}
}
//返回HTML
public function show() {
//$query_page = "{$_SERVER['PHP_SELF']}?";
//生成页面链接字符串
for($i = $this->head_page; $i <= $this->foot_page; $i++) {
if($i == $this->now_page) {
$this->str .= '<li class="current"><a>'.$i.'</a></li>';
} else {
$this->str .= '<li><a href="'.$this->query_page.'page='.$i.'">'.$i.'</a></li>';
}
}
//获得HTML
$this->result .= '<ul class="paginator">';
//$this->result .= '<li>共'.$this->pagesnum.'页/'.$this->total.'条记录</li>';
if($this->now_page != 1) {
$this->result .= '<li><a href="'.$this->query_page.'page=1">首页</a></li><li><a href="'.$this->query_page.'page='.($this->now_page - 1).'">上一页</a></li>';
}
$this->result .= $this->str;
if($this->now_page != $this->pagesnum) {
$this->result .= '<li><a href="'.$this->query_page.'page='.($this->now_page + 1).'">下一页</a></li><li><a href="'.$this->query_page.'page='.$this->pagesnum.'">末页</a></li>';
}
$this->result .= '</ul>';
if($this->pagesnum <= 1) {
return;
}
return $this->result;
}
}
?>
页:
[1]