当前位置:网站首页>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;边栏推荐
猜你喜欢
![[deep learning paper notes] hnf-netv2 for segmentation of brain tumors using multimodal MR imaging](/img/52/5e85743b1817de96a52e02b92fd08c.png)
[deep learning paper notes] hnf-netv2 for segmentation of brain tumors using multimodal MR imaging

龙芯派2代烧写PMON和重装系统

ELFK部署

What are the private addresses

记录一下在深度学习-一些bug处理

Huawei push service content, read notes

Usage, installation and use of TortoiseSVN

Fragmented knowledge management tool memos

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

Write API documents first or code first?
随机推荐
【MySQL 使用秘籍】一网打尽 MySQL 时间和日期类型与相关操作函数(三)
What happened to the communication industry in the first half of this year?
Operational research 68 | the latest impact factors in 2022 were officially released. Changes in journals in the field of rapid care
Godson 2nd generation burn PMON and reload system
TortoiseSVN使用情形、安装与使用
leetcode 10. Regular Expression Matching 正则表达式匹配 (困难)
Data Lake (VII): Iceberg concept and review what is a data Lake
[deep learning paper notes] hnf-netv2 for segmentation of brain tumors using multimodal MR imaging
NFT value and white paper acquisition
Binder communication process and servicemanager creation process
通讯录(链表实现)
When using Tencent cloud for the first time, you can only use webshell connection instead of SSH connection.
RK3566添加LED
Laravel framework operation error: no application encryption key has been specified
Solve the problem of invalid uni app configuration page and tabbar
Prefix, infix, suffix expression "recommended collection"
Aikesheng sqle audit tool successfully completed the evaluation of "SQL quality management platform grading ability" of the Academy of communications and communications
leetcode 10. Regular expression matching regular expression matching (difficult)
嵌入式软件架构设计-消息交互
A detailed explanation of ASCII code, Unicode and UTF-8