1 <?php
2 class page {
3 private $pageSize;
4 private $countNum;
5 private $pageCount;
6 private $currentPage;
7 private $showNumCount; //要显示的数字链接的个数
8 public $start; //开始位置
9 private $prev; //上一页链接
10 private $next; //下一页链接
11 private $num;
12 public $pageStr;
13 private $top;
14 private $bottom;
15 private $select;
16 private $url;
17
18 //构造函数
19 function __construct($countNum, $pageSize = 5) {
20 $this->url=$this->getURL();
21 $this->countNum = $countNum;
22 $this->pageSize = $pageSize;
23 $this->pageCount = ceil ( ($this->countNum) / ($this->pageSize) );
24 $this->currentPage = $this->getCurrentPage ();
25 $this->getStart ();
26 $this->getPagePrevAndNext ();
27 $this->getPageNum ();
28 $this->getTopAndBottom();
29 $this->select=$this->getSelect();
30
31 $this->mkPage();
32
33 }
34
35 //获得当前页函数
36 private function getCurrentPage() {
37 if (isset ( $_GET ["page"] ) && ! empty ( $_GET ["page"] )) {
38 $page = $_GET ["page"];
39 if ($page < 1) {
40 $page = 1;
41 }
42 if ($page > $this->pageCount) {
43 $page = $this->pageCount;
44 }
45 } else {
46 $page = 1;
47 }
48 return $page;
49 }
50
51 //获得开始位置函数
52 private function getStart() {
53 $this->start = ($this->currentPage - 1) * $this->pageSize;
54 }
55
56 //获得get字符串
57 private function getURL(){
58 $url="";
59
60 foreach ($_GET as $key=>$val){
61 if($key!="page"){
62 $url==""?$url.=$key."=".$val:$url.="&".$key."=".$val;
63 }
64 }
65 return $url."&";
66 }
67
68 //获得上一页下一页链接函数
69 private function getPagePrevAndNext() {
70 $pagePrev = $this->currentPage - 1>0?$this->currentPage-1:1;
71 $pageNext = $this->currentPage + 1>$this->pageCount?$this->pageCount:$this->currentPage+1;
72
73 $this->prev = "<a href='?{$this->url}page=$pagePrev'>上一页</a>";
74 $this->next = "<a href='?{$this->url}page=$pageNext'>下一页</a>";
75 }
76
77 //获得数字链接函数
78 public function getPageNum($showNumCount = 11) {
79 $str = "";
80 $this->showNumCount = $showNumCount;
81 if (($this->showNumCount) > ($this->pageCount)) {
82 for($i = 1; $i <= ($this->pageCount); $i ++) {
83 if ($i == $this->currentPage) {
84 $str .= $i . " ";
85 } else {
86 $str .= "<a href='?{$this->url}page=$i'>$i</a> ";
87 }
88 }
89 } else {
90 $step= round(($this->showNumCount-1)/2);
91 if (($this->currentPage) < $step||($this->currentPage)==$step) {
92 for($i = 1; $i <= $this->showNumCount; $i ++) {
93 if ($i == $this->currentPage) {
94 $str .= $i . " ";
95 } else {
96 $str .= "<a href='?{$this->url}page=$i'>$i</a> ";
97 }
98 }
99 } elseif ($step + ($this->currentPage) > $this->pageCount) {
100 for($i = ($this->pageCount - $this->showNumCount + 1); $i <= $this->pageCount; $i ++) {
101 if ($i == $this->currentPage) {
102 $str .= $i . " ";
103 } else {
104 $str .= "<a href='?{$this->url}page=$i'>$i</a> ";
105 }
106 }
107 } else {
108 for($i = $this->currentPage - $step; $i <= $this->currentPage + $step; $i ++) {
109 if ($i == $this->currentPage) {
110 $str .= $i . " ";
111 } else {
112 $str .= "<a href='?{$this->url}page=$i'>$i</a> ";
113 }
114 }
115 }
116 }
117 $this->num = $str;
118 }
119
120 //获得首位页链接
121 private function getTopAndBottom(){
122 $this->top=$this->currentPage==1?"":"<a href='?{$this->url}page=1'>首页</a>";
123 $this->bottom=$this->currentPage==$this->pageCount?"":"<a href='?{$this->url}page={$this->pageCount}'>尾页</a>";
124 }
125
126 //获得下拉列表链接
127 private function getSelect(){
128 $str="总共{$this->pageCount}页 ";
129 $str.="当前第";
130 $str.="<script type=\"text/javascript\">function selectsubmit(val){window.location.href='?{$this->url}page='+val}</script>
131 <select id=\"selectlist\" onchange=\"selectsubmit(this.value);\">";
132 for ($i=1;$i<=$this->pageCount;$i++){
133 $selected=$i==($this->currentPage)?"selected='selected'":"";
134 $str.="<option $selected value='$i'>$i</option>";
135 }
136 $str.="</select>";
137 $str.="页";
138 return $str;
139 }
140
141 //生成分页链接
142 function mkPage() {
143 $this->pageStr =$this->top." ". $this->prev . " " . $this->num . " " . $this->next." ".$this->bottom." {$this->select}";
144 }
145
146 }
147 ?>