<?php
/**
*
* 分页类
*/
class Page
{
public $num; //每页几条
public $total; //总条数
public $p; //当前页数
//构造方法
public function __construct($total, $num)
{
$this->total = $total;
$this->num = $num;
//默认为第一页
$this->p = isset($_GET['p']) ? $_GET['p'] : 1;
}
/**
* 获取limit 1 0,5 2 5,5 3 10,5 4 15,5
*/
public function getLimit()
{
$start = ($this->p-1)*$this->num;
$end = $this->num;
$limit = $start.','.$end;
return $limit;
}
/**
* 显示页码 上一页 1234567 下一页
*/
public function show()
{
echo '<pre>';
// var_dump($_SERVER['HTTP_REFERER']);
$url = $_SERVER['SCRIPT_NAME'];
//总页数
$totalPage = ceil($this->total/$this->num);
if ($this->p <= 1) {
$prev = 1;
} else {
$prev = $this->p-1;
}
//上一页
$str = '';
$str .= "<a href='$url?p=".$prev."'>上一页</a>";
//1234567891011
for ($i=1; $i <= $totalPage; $i++) {
if($i == $this->p) {
$str .= "<a href='$url?p={$i}' style='color:red'>第{$i}页</a>";
} else {
$str .= "<a href='$url?p={$i}'>第{$i}页</a>";
}
}
//下一页
if($this->p >= $totalPage) {
$next = $totalPage;
} else {
$next = $this->p+1;
}
$str .= "<a href='$url?p=".$next."'>下一页</a>";
return $str;
}
}
//实例化对象
//$page = new Page(38,5);
// $page->getLimit();
//echo $page->show();