当前位置:网站首页>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;
边栏推荐
- Address book (linked list implementation)
- jenkins安装
- [daily question] 1200 Minimum absolute difference
- Catch all asynchronous artifact completable future
- "Baidu Cup" CTF competition in September, web:upload
- Mmseg - Mutli view time series data inspection and visualization
- 今年上半年,通信行业发生了哪些事?
- redis6数据类型及操作总结
- 49. Grouping of alphabetic ectopic words: give you a string array, please combine the alphabetic ectopic words together. You can return a list of results in any order. An alphabetic ectopic word is a
- kafaka 日志收集
猜你喜欢
龙芯派2代烧写PMON和重装系统
Jetpack Compose入门到精通
Fragmented knowledge management tool memos
[public class preview]: basis and practice of video quality evaluation
[notes of in-depth study paper]transbtsv2: wider instead of deep transformer for medical image segmentation
Intranet penetration tool NetApp
华为推送服务内容,阅读笔记
Solve the problem of invalid uni app configuration page and tabbar
一网打尽异步神器CompletableFuture
[深度学习论文笔记]使用多模态MR成像分割脑肿瘤的HNF-Netv2
随机推荐
搭建一个仪式感点满的网站,并内网穿透发布到公网 2/2
法国学者:最优传输理论下对抗攻击可解释性探讨
Network security HSRP protocol
PostgreSQL Usage Summary (PIT)
Jasypt configuration file encryption | quick start | actual combat
go 字符串操作
go map
Parsing XML using Dom4j
Cloudcompare - point cloud slice
Win10 - lightweight gadget
Laravel framework operation error: no application encryption key has been specified
Data Lake (VII): Iceberg concept and review what is a data Lake
TortoiseSVN使用情形、安装与使用
Intranet penetration tool NetApp
Catch all asynchronous artifact completable future
go 数组与切片
NFT value and white paper acquisition
先写API文档还是先写代码?
leetcode 10. Regular Expression Matching 正则表达式匹配 (困难)
kafaka 日志收集