当前位置:网站首页>Simple PHP paging implementation
Simple PHP paging implementation
2022-07-05 13:42:00 【Black hearted green rose】
One 、 Method class
<?php
/**
* Paging class
*
* Call mode :
* $p=new Page( Total number of articles , Displays the number of pages , The current page number , Number of bars per page ,[ link ]);
* print_r($p->getPages()); // Generate an array of page numbers ( Key is page number , The value is link )
* echo $p->showPages(1); // Generate a page number style ( You can add custom styles )
*
*/
/*
Total number of articles , Number of pages to display , The current page , Number of entries per page , Connect
Generate a one-dimensional array , Key is page number The value is connection
Return a page number with a generated style ( And you can add styles according to your needs )
Default style common 45 Bar record , Each page shows 10 strip , Current 1/4 page [ home page ] [ page-up key ] [1] [2] [3] .. [ next page ] [ Tail page ]
*/
namespace app\atl\controller;
use think\Controller;
use think\Exception;
class Page extends controller{
private $myde_total; // Total number of records
private $myde_size; // The number of records displayed on a page
private $myde_page; // The current page
private $myde_page_count; // Total number of pages
private $myde_i; // Number of starting pages
private $myde_en; // End pages
private $myde_url; // Get current url
/*
* $show_pages
* Format of page display , The number of pages showing links is 2*$show_pages+1.
* Such as $show_pages=2 Then the page shows [ home page ] [ page-up key ] 1 2 3 4 5 [ next page ] [ Tail page ]
*/
private $show_pages;
public function __construct($myde_total = 1,$myde_size = 1, $myde_page = 1, $myde_url, $show_pages = 2) {
$this->myde_total = $this->numeric($myde_total);
$this->myde_size = $this->numeric($myde_size);
$this->myde_page = $this->numeric($myde_page);
$this->myde_page_count = ceil($this->myde_total / $this->myde_size);
$this->myde_url = $myde_url;
if ($this->myde_total < 0)
$this->myde_total = 0;
if ($this->myde_page < 1)
$this->myde_page = 1;
if ($this->myde_page_count < 1)
$this->myde_page_count = 1;
if ($this->myde_page > $this->myde_page_count)
$this->myde_page = $this->myde_page_count;
$this->limit = ($this->myde_page - 1) * $this->myde_size;
$this->myde_i = $this->myde_page - $show_pages;
$this->myde_en = $this->myde_page + $show_pages;
if ($this->myde_i < 1) {
$this->myde_en = $this->myde_en + (1 - $this->myde_i);
$this->myde_i = 1;
}
if ($this->myde_en > $this->myde_page_count) {
$this->myde_i = $this->myde_i - ($this->myde_en - $this->myde_page_count);
$this->myde_en = $this->myde_page_count;
}
if ($this->myde_i < 1)
$this->myde_i = 1;
}
// Detect whether it is a number
private function numeric($num) {
if (strlen($num)) {
if (!preg_match("/^[0-9]+$/", $num)) {
$num = 1;
} else {
$num = substr($num, 0, 11);
}
} else {
$num = 1;
}
return $num;
}
// Address replacement
private function page_replace($page) {
return str_replace("{page}", $page, $this->myde_url);
}
// home page
private function myde_home() {
if ($this->myde_page != 1) {
return "<a href='" . $this->page_replace(1) . "' title=' home page '> home page </a>";
} else {
return "<p> home page </p>";
}
}
// The previous page
private function myde_prev() {
if ($this->myde_page != 1) {
return "<a href='" . $this->page_replace($this->myde_page - 1) . "' title=' The previous page '> The previous page </a>";
} else {
return "<p> The previous page </p>";
}
}
// The next page
private function myde_next() {
if ($this->myde_page != $this->myde_page_count) {
return "<a href='" . $this->page_replace($this->myde_page + 1) . "' title=' The next page '> The next page </a>";
} else {
return"<p> The next page </p>";
}
}
// Tail page
private function myde_last() {
if ($this->myde_page != $this->myde_page_count) {
return "<a href='" . $this->page_replace($this->myde_page_count) . "' title=' Tail page '> Tail page </a>";
} else {
return "<p> Tail page </p>";
}
}
// Output
public function myde_write($id = 'page') {
$str = "<div id=" . $id . ">";
// $str.=$this->myde_home();
$str.=$this->myde_prev();
if ($this->myde_i > 1) {
$str.="<p class='pageEllipsis'>...</p>";
}
for ($i = $this->myde_i; $i <= $this->myde_en; $i++) {
if ($i == $this->myde_page) {
$str.="<a href='" . $this->page_replace($i) . "' title=' The first " . $i . " page ' class='cur'>$i</a>";
} else {
$str.="<a href='" . $this->page_replace($i) . "' title=' The first " . $i . " page '>$i</a>";
}
}
if ($this->myde_en < $this->myde_page_count) {
$str.="<p class='pageEllipsis'>...</p>";
}
$str.=$this->myde_next();
// $str.=$this->myde_last();
// $str.="<p class='pageRemark'> common <b>" . $this->myde_page_count .
// "</b> page <b>" . $this->myde_total . "</b> Data </p>";
$str.="</div>";
return $str;
}
}
?>Two 、 style
#page{
display: flex;
flex-wrap: wrap;
justify-content: center;
height:30px;
}
#page a{
display:block;
float:left;
margin-right:10px;
padding: 0 12px;
height: 30px;
border:1px #cccccc solid;
background:#fff;
text-decoration:none;
color:#808080;
font-size: 14px;
line-height: 30px;
}
#page a:hover{
color:#EB5C0D;
border:1px #EB5C0D solid;
}
#page a.cur{
border:none;
background:#EB5C0D;
color:#fff;
}
#page p{
float:left;
padding: 0 12px;
font-size:15px;
height: 30px;
line-height: 30px;
color:#bbb;
border:1px #ccc solid;
background:#fcfcfc;
margin-right:8px;
}
#page p.pageRemark{
border-style:none;
background:none;
margin-right:0px;
color:#666;
}
#page p.pageRemark b{
margin: 0 6px;
color: #EB5C0D;
}
#page p.pageEllipsis{
border-style:none;
background:none;
padding:4px 0px;
color:#808080;
}
.dates li {font-size: 14px;margin:20px 0}
.dates li span{float:right}3、 ... and 、 Reference method
$page = new \Page(' General record ',' Single page record ',' Current page ',' link ?page= The current page ');
$str = "<div id=" . $id . ">";
// $str.=$this->myde_home();
$str.=$this->myde_prev();
if ($this->myde_i > 1) {
$str.="<p class='pageEllipsis'>...</p>";
}
for ($i = $this->myde_i; $i <= $this->myde_en; $i++) {
if ($i == $this->myde_page) {
$str.="<a href='" . $this->page_replace($i) . "' title=' The first " . $i . " page ' class='cur'>$i</a>";
} else {
$str.="<a href='" . $this->page_replace($i) . "' title=' The first " . $i . " page '>$i</a>";
}
}
if ($this->myde_en < $this->myde_page_count) {
$str.="<p class='pageEllipsis'>...</p>";
}
$str.=$this->myde_next();
// $str.=$this->myde_last();
// $str.="<p class='pageRemark'> common <b>" . $this->myde_page_count .
// "</b> page <b>" . $this->myde_total . "</b> Data </p>";
$str.="</div>";
return $str;边栏推荐
- Flutter InkWell & Ink组件
- 多人合作项目查看每个人写了多少行代码
- Aspx simple user login
- Integer ==比较会自动拆箱 该变量不能赋值为空
- [深度学习论文笔记]TransBTSV2: Wider Instead of Deeper Transformer for Medical Image Segmentation
- “百度杯”CTF比赛 九月场,Web:Upload
- Laravel框架运行报错:No application encryption key has been specified
- 【 script secret pour l'utilisation de MySQL 】 un jeu en ligne sur l'heure et le type de date de MySQL et les fonctions d'exploitation connexes (3)
- Idea设置方法注释和类注释
- 面试官灵魂拷问:为什么代码规范要求 SQL 语句不要过多的 join?
猜你喜欢

“百度杯”CTF比赛 九月场,Web:SQL

Win10 - lightweight gadget

Android本地Sqlite数据库的备份和还原

Wonderful express | Tencent cloud database June issue

Huawei push service content, read notes

研究生可以不用学英语?只要考研英语或六级分数高!

搭建一个仪式感点满的网站,并内网穿透发布到公网 2/2

华为推送服务内容,阅读笔记

FPGA 学习笔记:Vivado 2019.1 添加 IP MicroBlaze

Shandong University Summer Training - 20220620
随机推荐
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
Godson 2nd generation burn PMON and reload system
Usage, installation and use of TortoiseSVN
面试官灵魂拷问:为什么代码规范要求 SQL 语句不要过多的 join?
Jenkins installation
FPGA 学习笔记:Vivado 2019.1 添加 IP MicroBlaze
Jetpack Compose入门到精通
Catch all asynchronous artifact completable future
Android本地Sqlite数据库的备份和还原
Flutter 3.0更新后如何应用到小程序开发中
Clock cycle
Matlab paper chart standard format output (dry goods)
PostgreSQL Usage Summary (PIT)
Zhubo Huangyu: these spot gold investment skills are not really bad
MySQL --- 数据库查询 - 排序查询、分页查询
asp. Net read TXT file
【 script secret pour l'utilisation de MySQL 】 un jeu en ligne sur l'heure et le type de date de MySQL et les fonctions d'exploitation connexes (3)
Kotlin协程利用CoroutineContext实现网络请求失败后重试逻辑
Could not set property 'ID' of 'class xx' with value 'XX' argument type mismatch solution
Solve the problem of invalid uni app configuration page and tabbar