php动态分页类
1 <?php
2
3 /**
4 * 頁(yè)面名稱(chēng):cls_page.php
5 */
6 class Page {
7 private $each_disNums; //每頁(yè)顯示的條目數(shù)
8 private $nums; //總條目數(shù)
9 private $current_page; //當(dāng)前被選中的頁(yè)
10 private $sub_pages; //每次顯示的頁(yè)數(shù)
11 private $pageNums; //總頁(yè)數(shù)
12 private $page_array = array (); //用來(lái)構(gòu)造分頁(yè)的數(shù)組
13 private $subPage_link; //每個(gè)分頁(yè)的鏈接
14
15
16 /**
17 *
18 * __construct是SubPages的構(gòu)造函數(shù),用來(lái)在創(chuàng)建類(lèi)的時(shí)候自動(dòng)運(yùn)行.
19 * @$each_disNums 每頁(yè)顯示的條目數(shù)
20 * @nums 總條目數(shù)
21 * @current_num 當(dāng)前被選中的頁(yè)
22 * @sub_pages 每次顯示的頁(yè)數(shù)
23 * @subPage_link 每個(gè)分頁(yè)的鏈接
24 * @subPage_type 顯示分頁(yè)的類(lèi)型
25 *
26 * 當(dāng)@subPage_type=1的時(shí)候?yàn)槠胀ǚ猪?yè)模式
27 * example: 共4523條記錄,每頁(yè)顯示10條,當(dāng)前第1/453頁(yè) [首頁(yè)] [上頁(yè)] [下頁(yè)] [尾頁(yè)]
28 * 當(dāng)@subPage_type=2的時(shí)候?yàn)榻?jīng)典分頁(yè)樣式
29 * example: 當(dāng)前第1/453頁(yè) [首頁(yè)] [上頁(yè)] 1 2 3 4 5 6 7 8 9 10 [下頁(yè)] [尾頁(yè)]
30 */
31 function __construct($each_disNums, $nums, $current_page, $sub_pages, $subPage_link) {
32 $this->each_disNums = intval($each_disNums);
33 $this->nums = intval($nums);
34 if (!$current_page) {
35 $this->current_page = 1;
36 } else {
37 $this->current_page = intval($current_page);
38 }
39 $this->sub_pages = intval($sub_pages);
40 $this->pageNums = ceil($nums / $each_disNums);
41 $this->subPage_link = $subPage_link;
42 }
43 /**
44 * 照顧低版本
45 */
46 /*
47 function __construct($each_disNums, $nums, $current_page, $sub_pages, $subPage_linke) {
48 $this->Page($each_disNums, $nums, $current_page, $sub_pages, $subPage_link);
49
50 }
51 */
52
53 /*
54 __destruct析構(gòu)函數(shù),當(dāng)類(lèi)不在使用的時(shí)候調(diào)用,該函數(shù)用來(lái)釋放資源。
55 */
56 function __destruct() {
57 unset ($each_disNums);
58 unset ($nums);
59 unset ($current_page);
60 unset ($sub_pages);
61 unset ($pageNums);
62 unset ($page_array);
63 unset ($subPage_link);
64 }
65
66 /*
67 用來(lái)給建立分頁(yè)的數(shù)組初始化的函數(shù)。
68 */
69 function initArray() {
70 for ($i = 0; $i < $this->sub_pages; $i++) {
71 $this->page_array[$i] = $i;
72 }
73 return $this->page_array;
74 }
75
76 /*
77 construct_num_Page該函數(shù)使用來(lái)構(gòu)造顯示的條目
78 即使:[1][2][3][4][5][6][7][8][9][10]
79 */
80 function construct_num_Page() {
81 if ($this->pageNums < $this->sub_pages) {
82 $current_array = array ();
83 for ($i = 0; $i < $this->pageNums; $i++) {
84 $current_array[$i] = $i +1;
85 }
86 } else {
87 $current_array = $this->initArray();
88 if ($this->current_page <= 3) {
89 for ($i = 0; $i < count($current_array); $i++) {
90 $current_array[$i] = $i +1;
91 }
92 }
93 elseif ($this->current_page <= $this->pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1) {
94 for ($i = 0; $i < count($current_array); $i++) {
95 $current_array[$i] = ($this->pageNums) - ($this->sub_pages) + 1 + $i;
96 }
97 } else {
98 for ($i = 0; $i < count($current_array); $i++) {
99 $current_array[$i] = $this->current_page - 2 + $i;
100 }
101 }
102 }
103
104 return $current_array;
105 }
106
107 /*
108 構(gòu)造普通模式的分頁(yè)
109 共4523條記錄,每頁(yè)顯示10條,當(dāng)前第1/453頁(yè) [首頁(yè)] [上頁(yè)] [下頁(yè)] [尾頁(yè)]
110 */
111 function subPageCss1() {
112 $subPageCss1Str = "";
113 $subPageCss1Str .= "共" . $this->nums . "條記錄,";
114 $subPageCss1Str .= "每頁(yè)顯示" . $this->each_disNums . "條,";
115 $subPageCss1Str .= "當(dāng)前第" . $this->current_page . "/" . $this->pageNums . "頁(yè) ";
116 if ($this->current_page > 1) {
117 $firstPageUrl = $this->subPage_link . "1";
118 $prewPageUrl = $this->subPage_link . ($this->current_page - 1);
119 $subPageCss1Str .= "[<a href='$firstPageUrl'>首頁(yè)</a>] ";
120 $subPageCss1Str .= "[<a href='$prewPageUrl'>上一頁(yè)</a>] ";
121 } else {
122 $subPageCss1Str .= "[首頁(yè)] ";
123 $subPageCss1Str .= "[上一頁(yè)] ";
124 }
125
126 if ($this->current_page < $this->pageNums) {
127 $lastPageUrl = $this->subPage_link . $this->pageNums;
128 $nextPageUrl = $this->subPage_link . ($this->current_page + 1);
129 $subPageCss1Str .= " [<a href='$nextPageUrl'>下一頁(yè)</a>] ";
130 $subPageCss1Str .= "[<a href='$lastPageUrl'>尾頁(yè)</a>] ";
131 } else {
132 $subPageCss1Str .= "[下一頁(yè)] ";
133 $subPageCss1Str .= "[尾頁(yè)] ";
134 }
135
136 return $subPageCss1Str;
137
138 }
139
140 /*
141 構(gòu)造經(jīng)典模式的分頁(yè)
142 當(dāng)前第1/453頁(yè) [首頁(yè)] [上頁(yè)] 1 2 3 4 5 6 7 8 9 10 [下頁(yè)] [尾頁(yè)]
143 */
144 function subPageCss2() {
145 $subPageCss2Str = "";
146 $subPageCss2Str .= "當(dāng)前第" . $this->current_page . "/" . $this->pageNums . "頁(yè) ";
147
148 if ($this->current_page > 1) {
149 $firstPageUrl = $this->subPage_link . "1";
150 $prewPageUrl = $this->subPage_link . ($this->current_page - 1);
151 $subPageCss2Str .= "[<a href='$firstPageUrl'>首頁(yè)</a>] ";
152 $subPageCss2Str .= "[<a href='$prewPageUrl'>上一頁(yè)</a>] ";
153 } else {
154 $subPageCss2Str .= "[首頁(yè)] ";
155 $subPageCss2Str .= "[上一頁(yè)] ";
156 }
157
158 $a = $this->construct_num_Page();
159 for ($i = 0; $i < count($a); $i++) {
160 $s = $a[$i];
161 if ($s == $this->current_page) {
162 $subPageCss2Str .= "[<span style='color:red;font-weight:bold;'>" . $s . "</span>]";
163 } else {
164 $url = $this->subPage_link . $s;
165 $subPageCss2Str .= "[<a href='$url'>" . $s . "</a>]";
166 }
167 }
168
169 if ($this->current_page < $this->pageNums) {
170 $lastPageUrl = $this->subPage_link . $this->pageNums;
171 $nextPageUrl = $this->subPage_link . ($this->current_page + 1);
172 $subPageCss2Str .= " [<a href='$nextPageUrl'>下一頁(yè)</a>] ";
173 $subPageCss2Str .= "[<a href='$lastPageUrl'>尾頁(yè)</a>] ";
174 } else {
175 $subPageCss2Str .= "[下一頁(yè)] ";
176 $subPageCss2Str .= "[尾頁(yè)] ";
177 }
178 return $subPageCss2Str;
179 }
180 }
181
182
183 ?>
184
185
186
187 <?
188 class sqldao{
189 //連接數(shù)據(jù)庫(kù)
190 function conn(){
191 $dbh=mysql_connect('localhost','username','pass')or die("對(duì)不起,數(shù)據(jù)庫(kù)連接錯(cuò)誤!請(qǐng)稍候再來(lái),或與管理員聯(lián)系");
192 mysql_query("set names 'utf8'", $dbh);
193 mysql_select_db('tableName');
194 return $dbh;
195 }
196 }
197 ?>
198
199
200
201 <?
202 //以下為測(cè)試代碼
203 $sqldao=new sqldao();
204 $dbh=$sqldao->conn();
205
206 $start=($_GET['p']-1)*10;
207 $que="select utname from renyuan";
208 $rs=mysql_query($que,$dbh);
209 $num_all=mysql_num_rows($rs);
210
211 $rs=mysql_query($que." limit ".$start.",10",$dbh);
212 $num=mysql_num_rows($rs);
213 for($i=0;$i<$num;$i++){
214 $row=mysql_fetch_array($rs);
215 echo $row['utname']."<br>";
216 }
217
218
219 echo "<hr>";
220 //測(cè)試一下,看看兩種不同效果
221 //$t = new Page(顯示條數(shù), 數(shù)據(jù)總數(shù), 頁(yè)數(shù), 頁(yè)數(shù)鏈接個(gè)數(shù), '頁(yè)面鏈接');
222 $t = new Page(10, $num_all, $_GET['p'], 10, 'cls_page.php?p=');
223 echo $t->subPageCss2();
224 echo "<p>";
225 echo $t->subPageCss1();
226
227 ?>
?
轉(zhuǎn)載于:https://www.cnblogs.com/huanglibin/p/3502320.html
總結(jié)
- 上一篇: 关于错误error C4430 erro
- 下一篇: thinkphp 编辑器kindedit