当前位置:网站首页>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;
边栏推荐
- Catch all asynchronous artifact completable future
- 通讯录(链表实现)
- MMSeg——Mutli-view时序数据检查与可视化
- redis6主从复制及集群
- asp. Net read TXT file
- 多人合作项目查看每个人写了多少行代码
- These 18 websites can make your page background cool
- kafaka 日志收集
- 记录一下在深度学习-一些bug处理
- Aikesheng sqle audit tool successfully completed the evaluation of "SQL quality management platform grading ability" of the Academy of communications and communications
猜你喜欢
[notes of in-depth study paper]transbtsv2: wider instead of deep transformer for medical image segmentation
Idea set method annotation and class annotation
MySQL - database query - sort query, paging query
那些考研后才知道的事
Aikesheng sqle audit tool successfully completed the evaluation of "SQL quality management platform grading ability" of the Academy of communications and communications
华为推送服务内容,阅读笔记
Cloudcompare - point cloud slice
ELFK部署
FPGA learning notes: vivado 2019.1 add IP MicroBlaze
What are the private addresses
随机推荐
kafaka 日志收集
研究生可以不用学英语?只要考研英语或六级分数高!
Flutter 3.0更新后如何应用到小程序开发中
Pancake Bulldog robot V2 (code optimized)
什么叫做信息安全?包含哪些内容?与网络安全有什么区别?
FPGA 学习笔记:Vivado 2019.1 添加 IP MicroBlaze
53. Maximum subarray sum: give you an integer array num, please find a continuous subarray with the maximum sum (the subarray contains at least one element) and return its maximum sum.
The "Baidu Cup" CTF competition was held in February 2017, Web: explosion-2
通讯录(链表实现)
MMSeg——Mutli-view时序数据检查与可视化
记录一下在深度学习-一些bug处理
Shuttle INKWELL & ink components
redis6主从复制及集群
Record in-depth learning - some bug handling
内网穿透工具 netapp
Win10——轻量级小工具
南理工在线交流群
Personal component - message prompt
ETCD数据库源码分析——集群间网络层客户端peerRt
Solve the problem of "unable to open source file" xx.h "in the custom header file on vs from the source