-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagination.php
112 lines (82 loc) · 2.88 KB
/
pagination.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
namespace database;
/**
* This file is part of keenly from.
* @author brain_yang<[email protected]>
* (c) brain_yang
* github: https://github.com/keenlysoft/
* @time 2018年10月27日
* For the full copyright and license information, please view the LICENSE
*/
use keenly;
class pagination{
private $count;
private $total;
private $page;
private $number;
private $totalPage;
public $limit;
public $offset;
public function __construct($total,$page = 1,$number = 10){
$this->total = $total;
$this->page = isset($page)?$page:1;
$this->number = $number;
$this->calculate();
}
function page(){
$html = "<div>";
$html .= "<a class='prev' href='{$this->previous()}'><<</a>";
$toPage = $this->toPage();
for ($i = $toPage['min'];$i <= $toPage['max'];$i++)
{
$url = $this->preg_url($i);
if($this->page == $i){
$html .='<span class="current">'.$i.'</span>';
}else{
$html .="<a class='num' href='$url'>$i</a>";
}
}
if(($i-1) != $this->totalPage){
$html .="<a class='num' title= '总页码' href='{$this->totalPage()}'>{$this->totalPage}</a>";
}
$html .= "<a class='next' href='{$this->next()}' >>></a> ";
$html .= "</div>";
return $html;
}
function calculate(){
$this->totalPage = ceil(bcdiv($this->total, $this->number,1));
if($this->totalPage <= 0 || $this->page <= 1){
$this->limit = $this->number;
$this->offset = 0;
}else{
$this->limit = $this->number;
$this->offset = bcmul(($this->page>1?($this->page-1):$this->page), $this->number);
}
}
private function preg_url($i){
$replace = preg_replace("/page=([0-9]+)/","page={$i}",\keenly::$box->url->web);
if(!strpos($replace,'?')){
return $replace.'?page='.$i;
}elseif (!strpos($replace,'page')){
return $replace.'&page='.$i;
}else{
return $replace;
}
}
private function previous(){
$previous = ($this->page-1)<1?1:($this->page-1);
return $this->preg_url($previous);
}
private function next(){
$next = ($this->page+1) >= $this->totalPage?$this->totalPage:($this->page+1);
return $this->preg_url($next);
}
private function totalPage(){
return $this->preg_url($this->totalPage);
}
private function toPage(){
$minPage = bcsub($this->page,3)<1?1:bcsub($this->page,3);
$maxPage = bcadd($this->page,6) >= $this->totalPage?$this->totalPage:bcadd($this->page,6);
return ['min'=>$minPage,'max'=>$maxPage];
}
}