当前位置:网站首页>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;
边栏推荐
- 先写API文档还是先写代码?
- The real king of caching, Google guava is just a brother
- 【MySQL 使用秘籍】一网打尽 MySQL 时间和日期类型与相关操作函数(三)
- These 18 websites can make your page background cool
- The development of speech recognition app with uni app is simple and fast.
- 【MySQL 使用秘籍】一網打盡 MySQL 時間和日期類型與相關操作函數(三)
- [深度学习论文笔记]UCTransNet:从transformer的通道角度重新思考U-Net中的跳跃连接
- MySQL - database query - sort query, paging query
- mysql获得时间
- Android本地Sqlite数据库的备份和还原
猜你喜欢
Huawei push service content, read notes
一文详解ASCII码,Unicode与utf-8
Idea设置方法注释和类注释
Can and can FD
Aikesheng sqle audit tool successfully completed the evaluation of "SQL quality management platform grading ability" of the Academy of communications and communications
Wonderful express | Tencent cloud database June issue
Win10 - lightweight gadget
[深度学习论文笔记]使用多模态MR成像分割脑肿瘤的HNF-Netv2
内网穿透工具 netapp
[public class preview]: basis and practice of video quality evaluation
随机推荐
[deep learning paper notes] hnf-netv2 for segmentation of brain tumors using multimodal MR imaging
ETCD数据库源码分析——rawnode简单封装
STM32 reverse entry
Fragmented knowledge management tool memos
【MySQL 使用秘籍】一網打盡 MySQL 時間和日期類型與相關操作函數(三)
Android本地Sqlite数据库的备份和还原
leetcode 10. Regular expression matching regular expression matching (difficult)
Could not set property ‘id‘ of ‘class XX‘ with value ‘XX‘ argument type mismatch 解决办法
【Hot100】33. Search rotation sort array
jenkins安装
多人合作项目查看每个人写了多少行代码
Wonderful express | Tencent cloud database June issue
Record in-depth learning - some bug handling
49. 字母异位词分组:给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。 字母异位词 是由重新排列源单词的字母得到的一个新单词,所有源单词中的字母通常恰好只用一次。
Multi person cooperation project to see how many lines of code each person has written
Kafaka log collection
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.
[深度学习论文笔记]使用多模态MR成像分割脑肿瘤的HNF-Netv2
With 4 years of working experience, you can't tell five ways of communication between multithreads. Dare you believe it?
Idea设置方法注释和类注释