PHP通用分页(Pager)类

三种不同展示方式 附上style~

1. 效果图1

2.效果图2 

3. 效果图3

4. 分页类主体
001 <?php
002 /**
003  * PHP通用分页类
004  *
005  * show(2) 1 ... 62 63 64 65 66 67 68 ... 150
006  * 分页样式
007  * #page{font:12px/16px arial}
008  * #page span{float:left;margin:0px 3px;}
009  * #page a{float:left;margin:0 3px;border:1px solid #ddd;padding:3px
010  * 7px; text-decoration:none;color:#666}
011  * #page a.now_page,#page a:hover{color:#fff;background:#05c}
012  */
013 class Pager {
014     public $first_row//起始行数
015     public $list_rows//列表每页显示行数
016     protected $total_pages//总页数
017     protected $total_rows//总行数
018     protected $now_page//当前页数
019     protected $method 'defalut'//处理情况 Ajax分页 Html分页(静态化时) 普通get方式
020     protected $parameter '';
021     protected $page_name//分页参数的名称
022     protected $ajax_func_name;
023     public $plus = 3; //分页偏移量
024     protected $url;
025  
026     /**
027      * 构造函数
028      *
029      * @param unknown_type $data
030      */
031     public function __construct($data array()) {
032         $this->total_rows = $data['total_rows'];
033  
034         $this->parameter = !empty($data['parameter']) ? $data['parameter'] : '';
035         $this->list_rows = !empty($data['list_rows']) && $data['list_rows'] <= 100 ? $data['list_rows'] : 15;
036         $this->total_pages = ceil($this->total_rows / $this->list_rows);
037         $this->page_name = !empty($data['page_name']) ? $data['page_name'] : 'page';
038         $this->ajax_func_name = !empty($data['ajax_func_name']) ? $data['ajax_func_name'] : '';
039  
040         $this->method = !empty($data['method']) ? $data['method'] : '';
041  
042         /* 当前页面 */
043         if (!empty($data['now_page'])) {
044             $this->now_page = intval($data['now_page']);
045         else {
046             $this->now_page = !empty($_GET[$this->page_name]) ? intval($_GET[$this->page_name]) : 1;
047         }
048         $this->now_page = $this->now_page <= 0 ? 1 : $this->now_page;
049  
050         if (!empty($this->total_pages) && $this->now_page > $this->total_pages) {
051             $this->now_page = $this->total_pages;
052         }
053         $this->first_row = $this->list_rows * ($this->now_page - 1);
054     }
055  
056     /**
057      * 得到当前连接
058      *
059      * @param
060      *          $page
061      * @param
062      *          $text
063      * @return string
064      */
065     protected function _get_link($page$text) {
066         switch ($this->method) {
067             case 'ajax' :
068                 $parameter '';
069                 if ($this->parameter) {
070                     $parameter ',' $this->parameter;
071                 }
072                 return '<a onclick="' $this->ajax_func_name . '('' . $page . ''' $parameter ')" href="javascript:void(0)">' $text '</a>' . "
073 ";
074                 break;
075  
076             case 'html' :
077                 $url str_replace('?'$page$this->parameter);
078                 return '<a href="' $url '">' $text '</a>' . "
079 ";
080                 break;
081  
082             default :
083                 return '<a href="' $this->_get_url($page) . '">' $text '</a>' . "
084 ";
085                 break;
086         }
087     }
088  
089     /**
090      * 设置当前页面链接
091      */
092     protected function _set_url() {
093         $url $_SERVER['REQUEST_URI'] . (strpos($_SERVER['REQUEST_URI'], '?') ? '' "?") . $this->parameter;
094         $parse parse_url($url);
095         if (isset($parse['query'])) {
096             parse_str($parse['query'], $params);
097             unset($params[$this->page_name]);
098             $url $parse['path'] . '?' . http_build_query($params);
099         }
100         if (!empty($params)) {
101             $url .= '&';
102         }
103         $this->url = $url;
104     }
105  
106     /**
107      * 得到$page的url
108      *
109      * @param $page 页面
110      * @return string
111      */
112     protected function _get_url($page) {
113         if ($this->url === NULL) {
114             $this->_set_url();
115         }
116         //  $lable = strpos('&', $this->url) === FALSE ? '' : '&';
117         return $this->url . $this->page_name . '=' $page;
118     }
119  
120     /**
121      * 得到第一页
122      *
123      * @return string
124      */
125     public function first_page($name '第一页') {
126         if ($this->now_page > 5) {
127             return $this->_get_link('1'$name);
128         }
129         return '';
130     }
131  
132     /**
133      * 最后一页
134      *
135      * @param
136      *          $name
137      * @return string
138      */
139     public function last_page($name '最后一页') {
140         if ($this->now_page < $this->total_pages - 5) {
141             return $this->_get_link($this->total_pages, $name);
142         }
143         return '';
144     }
145  
146     /**
147      * 上一页
148      *
149      * @return string
150      */
151     public function up_page($name '上一页') {
152         if ($this->now_page != 1) {
153             return $this->_get_link($this->now_page - 1, $name);
154         }
155         return '';
156     }
157  
158     /**
159      * 下一页
160      *
161      * @return string
162      */
163     public function down_page($name '下一页') {
164         if ($this->now_page < $this->total_pages) {
165             return $this->_get_link($this->now_page + 1, $name);
166         }
167         return '';
168     }
169  
170     /**
171      * 分页样式输出
172      *
173      * @param
174      *          $param
175      * @return string
176      */
177     public function show($param = 1) {
178         if ($this->total_rows < 1) {
179             return '';
180         }
181  
182         $className 'show_' $param;
183  
184         $classNames = get_class_methods($this);
185  
186         if (in_array($className$classNames)) {
187             return $this->$className();
188         }
189         return '';
190     }
191  
192     protected function show_2() {
193         if ($this->total_pages != 1) {
194             $return '';
195             $return .= $this->up_page('<');
196             for ($i = 1; $i <= $this->total_pages; $i++) {
197                 if ($i == $this->now_page) {
198                     $return .= "<a class='now_page'>$i</a>
199 ";
200                 else {
201                     if ($this->now_page - $i >= 4 && $i != 1) {
202                         $return .= "<span class='pageMore'>...</span>
203 ";
204                         $i $this->now_page - 3;
205                     else {
206                         if ($i >= $this->now_page + 5 && $i != $this->total_pages) {
207                             $return .= "<span>...</span>
208 ";
209                             $i $this->total_pages;
210                         }
211                         $return .= $this->_get_link($i$i) . "
212 ";
213                     }
214                 }
215             }
216             $return .= $this->down_page('>');
217             return $return;
218         }
219     }
220  
221     protected function show_1() {
222         $plus $this->plus;
223         if ($plus $this->now_page > $this->total_pages) {
224             $begin $this->total_pages - $plus * 2;
225         else {
226             $begin $this->now_page - $plus;
227         }
228  
229         $begin = ($begin >= 1) ? $begin : 1;
230         $return '';
231         $return .= $this->first_page();
232         $return .= $this->up_page();
233         for ($i $begin$i <= $begin $plus * 2; $i++) {
234             if ($i $this->total_pages) {
235                 break;
236             }
237             if ($i == $this->now_page) {
238                 $return .= "<a class='now_page'>$i</a>
239 ";
240             else {
241                 $return .= $this->_get_link($i$i) . "
242 ";
243             }
244         }
245         $return .= $this->down_page();
246         $return .= $this->last_page();
247         return $return;
248     }
249  
250     protected function show_3() {
251         $plus $this->plus;
252         if ($plus $this->now_page > $this->total_pages) {
253             $begin $this->total_pages - $plus * 2;
254         else {
255             $begin $this->now_page - $plus;
256         }
257         $begin = ($begin >= 1) ? $begin : 1;
258         $return '总计 ' $this->total_rows . ' 个记录分为 ' $this->total_pages . ' 页, 当前第 ' $this->now_page . ' 页 ';
259         $return .= ',每页 ';
260         $return .= '<input type="text" value="' $this->list_rows . '" id="pageSize" size="3"> ';
261         $return .= $this->first_page() . "
262 ";
263         $return .= $this->up_page() . "
264 ";
265         $return .= $this->down_page() . "
266 ";
267         $return .= $this->last_page() . "
268 ";
269         $return .= '<select onchange="' $this->ajax_func_name . '(this.value)" id="gotoPage">';
270  
271         for ($i $begin$i <= $begin + 10; $i++) {
272             if ($i $this->total_pages) {
273                 break;
274             }
275             if ($i == $this->now_page) {
276                 $return .= '<option selected="true" value="' $i '">' $i '</option>';
277             else {
278                 $return .= '<option value="' $i '">' $i '</option>';
279             }
280         }
281         $return .= '</select>';
282         return $return;
283     }
284 }
285  
286 ?>


5.使用方法
01 ###处理html静态化页面分页的情况###
02 # method 处理环境 设置为 html
03 # parameter 为静态页面参数  xxx.com/20-0-0-0-40-?.html 注意问号
04 # ?问号的位置会自动替换为去向页码
05 # now_page 当前页面(静态页面获取不到当前页面所以只有你传入)
06 $params array(
07             'total_rows'=>100, #(必须)
08             'method'    =>'html', #(必须)
09             'parameter' =>'xxx.com/20-0-0-0-40-?.html',  #(必须)
10             'now_page'  =>$_GET['p'],  #(必须)
11             'list_rows' =>10, #(可选) 默认为15
12 );
13 $page new Pager($params);
14    echo  $page->show(1);
15    #<a href="xxx.com/20-0-0-0-40-2.html">2</a>
16  
17 ###处理ajax分页的情况###
18 # method 处理环境 设置为 ajax
19 # ajax_func_name ajax分页跳转页面的javascript方法
20 # parameter    ajax_func_name后面的附带参数 默认为空
21 # now_page 不到当前页面所以只有你传入
22 $params array(
23             'total_rows'=>100,
24             'method'    =>'ajax',
25             'ajax_func_name' =>'goToPage',
26             'now_page'  =>1,
27             #'parameter' =>"'jiong','username'",
28 );
29 $page new Pager($params);
30    echo  $page->show(1);
31 #<a href="javascript:void(0)" onclick="goToPage('7')">7</a>
32 #添加了parameter<a href="javascript:void(0)" onclick="goToPage('6','jiong','username')">6</a>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值