Page类php
<?php
class Page
{
//超链接
protected $url;
//总条数
protected $total;
//总页数
protected $count;
//每页显示数
protected $num;
//上一页数
protected $prevNum;
//下一页数
protected $nextNum;
//开始记录数
protected $startNum;
//结束记录数
protected $endNum;
//首页
protected $first = '首页';
//尾页
protected $last = '尾页';
//上一页
protected $up = '上一页';
//下一页
protected $down = '下一页';
//当前页
protected $page;
//初始化成员属性
public function __construct($url , $total , $num = 5)
{
$this->url = $url;
$this->total = $total; //总条数
$this->num = $num;
//当前页
$this->page = isset($_GET['page']) ? $_GET['page'] : 1;
//求出总页数
$this->count = $this->getCount();
//上一页数
$this->prevNum = $this->getPrev();
//下一页
$this->nextNum = $this->getNext();
//开始记录数
$this->startNum = $this->getStart();
//结束记录数
$this->endNum = $this->getEnd();
}
//求出总页数
protected function getCount()
{
return ceil($this->total / $this->num);
}
//上一页数
protected function getPrev()
{
if ($this->page < 1) {
return false;
} else {
return $this->page - 1;
}
}
//下一页
protected function getNext()
{
if ($this->page >= $this->count) {
return false;
} else {
return $this->page + 1;
}
}
//开始记录数
protected function getStart()
{
return ($this->page - 1) * $this->num + 1;
}
//结束记录数
protected function getEnd()
{
return min($this->page * $this->num , $this->total);
}
//获取偏移量
public function getOffset()
{
return ($this->page - 1) * $this->num;
}
//获取分页
//当前是第X页,共X页,从第X条记录到第X条记录,首页,上一页,下一页 尾页
public function getPage()
{
$string = '';
$string .= '当前是'.$this->page.'页 共'.$this->count.'页 从第'.$this->startNum.'记录到第'.$this->endNum.'条记录 <a href="'.$this->url.'page=1">'.$this->first.'</a> ';
//上一页
if ($this->prevNum) {
$string .= '<a href="'.$this->url.'page='.$this->prevNum.'">'.$this->up.'</a> ';
}
//下一页
if ($this->nextNum) {
$string .= '<a href="'.$this->url.'page='.$this->nextNum.'">'.$this->down.'</a> ';
}
//拼接尾页
$string .= '<a href="'.$this->url.'page='.$this->count.'">'.$this->last.'</a>';
return $string;
}
}
$p = new Page('http://localhost/1606/5/page.php?' , 50);
$p->getPage();
调用page类php
<?php
$link = mysqli_connect('localhost' , 'root' , 'kungezuishuai');
mysqli_set_charset($link , 'utf8');
mysqli_select_db($link , 'bbs');
$sql = 'select count(*) as count from bbs_user';
$aRes = mysqli_query($link , $sql);
$data = mysqli_fetch_assoc($aRes);
//求总数
$count = $data['count'];
include 'page.php';
$page = new Page('http://localhost/1606/5/fy.php?' , $count , 2);
$offset = $page->getOffset();
$sql = "select * from bbs_user limit $offset , 2";
$result = mysqli_query($link , $sql);
if ($result && mysqli_affected_rows($link)) {
echo '<table width="500" border="1">';
while($rows = mysqli_fetch_assoc($result)) {
echo '<tr>';
echo '<td>'.$rows['id'].'</td>';
echo '<td>'.$rows['username'].'</td>';
echo '<td>'.$rows['email'].'</td>';
echo '<td>'.date('Y-m-d H:i:s' , $rows['ctime']).'</td>';
echo '</tr>';
}
echo '</table>';
}
echo $page->getPage();