[php]
/*
思路
1.把地址栏的URL获取
2.分析URL中的query部分--就是?后面传参数的部分
3.query部分分析成数组
4.把数组中的page单元,+1,-1,形成2个新的数组
5.再把新数组拼接成query部分,合成上一页,下一页连接地址
*/
//分页类
class Page {
public $total; //全部条数,从数据库取出
public $prePage = 10; //每页的条数
protected $curr= 1; //默认当前页码
public function __construct($total,$prePage='') {
$this->total = $total; //把总条目信息放在total属性
if ($prePage > 0) {
$this->prePage = $prePage; //把每页数量放在perPage属性
}
//计算当前页码
if (isset($_GET['page']) && ($_GET['page'] + 0) > 0) {
$this->curr = $_GET['page'] + 0;
}
}
//主体函数
public function showPage() {
if ($this->total
return ''; //如果总条目
}
$cnt = ceil($this->total / $this->prePage); //算总页数,进一取整
//根据当前页,算上一页,下一页
/*
分析url,有几种情况?
xx.php
xx.php?id=5
xx.php?page=3
xx.php?id=5&page=3
*/
//最终生成的URL里边必然有page=N
$url = $_SERVER['REQUEST_URI'];
$parse = parse_url($url); //把URL分析结果放在数组里
//print_r($parse);
//保证参数里边有page
if (!isset($parse['query'])) {
$parse['query'] = 'page=' .$this->curr;
}
//把query字符串分析成数组,再次确保有page选项
parse_str($parse['query'],$parms);
if (!array_key_exists('page', $parms)) {
$parms['page'] = $this->curr;
}
//上边四种情况都测试一遍,page参数都能生成
//print_r($parms);
//判断除了page之外,还有没有其他参数
if (count($parms) == 1) {
$url = $parse['path'] . '?';
} else {
unset($parms['page']);
$url = $parse['path'] . '?' . http_build_query($parms) . '&';
}
//echo $url
$prev = $this->curr - 1;
$next = $this->curr + 1;
//首页
$indexLink = '首页';
//上一页
if ($prev
$prevLink = '';
}else {
$prevLink = '上一页';
}
//下一页
if ($next > $cnt) {
$nextLink = '';
}else {
$nextLink = '下一页';
}
//尾页
$lastLink = '尾页';
//echo $indexLink.' '.$prevLink.' '.$nextLink .' '.$lastLink;
//上一页,1 2 3 4 5 下一页
$start = $this->curr - (5-1)/2; //计算左侧开始的页码
$end = $this->curr + (5-1)/2; //计算右侧开始的页码
//如果左侧的页面,已经小于1,则把小于1 的部分补到右侧
if ($start
$end += (1 - $start);
$start = 1; //修改start = 1
if ($end > $cnt) {
$end = $cnt;
}
}
//把右侧超出的部分,补到左边
if ($end > $cnt) {
$start -= ($end - $cnt);
$end = $cnt;
if ($start
$start = 1;
}
}
//循环出页码数
$pageStr = '';
for ($i=$start; $i
if ($i == $this->curr) {
$pageStr .= $i;
continue;
}
$pageStr .= '' . $i . '';
}
return $indexLink.$prevLink.$pageStr.$nextLink.$lastLink;
}
}
$page = new Page(30,3);
echo $page->showPage();

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools
