超强PHP分页类(转自PHPCHINA)
Release: 2016-07-11 20:00:38
Original
1219 people have browsed it
1
2/**
3 * filename: ext_page.class.php
4 * @package:phpbean
5 * @author :feifengxlq
6 * @copyright :Copyright 2006 feifengxlq
7 * @license:version 2.0
8 * @create:2006-5-31
9 * @modify:2006-6-1
10 * @modify:feifengxlq 2006-11-4
11 * description:超强分页类,四种分页模式,默认采用类似baidu,google的分页风格。
12 * 2.0增加功能:支持自定义风格,自定义样式,同时支持PHP4和PHP5,
13 * to see detail,please visit [url=http://www.phpobject.net/blog/read.php]http://www.phpobject.net/blog/read.php[/url]?
14 * example:
15 * 模式四种分页模式:
16 require_once('../libs/classes/page.class.php');
17 $page=new page(array('total'=>1000,'perpage'=>20));
18 echo 'mode:1
'.$page->show();
19 echo '
mode:2
'.$page->show(2);
20 echo '
mode:3
'.$page->show(3);
21 echo '
mode:4
'.$page->show(4);
22 开启AJAX:
23 $ajaxpage=new page(array('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test'));
24 echo 'mode:1
'.$ajaxpage->show();
25 采用继承自定义分页显示模式:
26 demo:http://www.phpobject.net/blog
27 */
28class page
29{
30 /**
31 * config ,public
32 */
33 var $page_name="PB_page";//page标签,用来控制url页。比如说xxx.php?PB_page=2中的PB_page
34 var $next_page='>';//下一页
35 var $pre_page='';//上一页
36 var $first_page='First';//首页
37 var $last_page='Last';//尾页
38 var $pre_bar='';//上一分页条
39 var $next_bar='>>';//下一分页条
40 var $format_left='[';
41 var $format_right=']';
42 var $is_ajax=false;//是否支持AJAX分页模式
43
44 /**
45 * private
46 *
47 */
48 var $pagebarnum=10;//控制记录条的个数。
49 var $totalpage=0;//总页数
50 var $ajax_action_name='';//AJAX动作名
51 var $nowindex=1;//当前页
52 var $url="";//url地址头
53 var $offset=0;
54
55 /**
56 * constructor构造函数
57 *
58 * @param array $array['total'],$array['perpage'],$array['nowindex'],$array['url'],$array['ajax']
59 */
60 function page($array)
61 {
62 if(is_array($array)){
63 if(!array_key_exists('total',$array))$this->error(__FUNCTION__,'need a param of total');
64 $total=intval($array['total']);
65 $perpage=(array_key_exists('perpage',$array))?intval($array['perpage']):10;
66 $nowindex=(array_key_exists('nowindex',$array))?intval($array['nowindex']):'';
67 $url=(array_key_exists('url',$array))?$array['url']:'';
68 }else{
69 $total=$array;
70 $perpage=10;
71 $nowindex='';
72 $url='';
73 }
74 if((!is_int($total))||($total0))$this->error(__FUNCTION__,$total.' is not a positive integer!');
75 if((!is_int($perpage))||($perpage0))$this->error(__FUNCTION__,$perpage.' is not a positive integer!');
76 if(!empty($array['page_name']))$this->set('page_name',$array['page_name']);//设置pagename
77 $this->_set_nowindex($nowindex);//设置当前页
78 $this->_set_url($url);//设置链接地址
79 $this->totalpage=ceil($total/$perpage);
80 $this->offset=($this->nowindex-1)*$this->perpage;
81 if(!empty($array['ajax']))$this->open_ajax($array['ajax']);//打开AJAX模式
82 }
83 /**
84 * 设定类中指定变量名的值,如果改变量不属于这个类,将throw一个exception
85 *
86 * @param string $var
87 * @param string $value
88 */
89 function set($var,$value)
90 {
91 if(in_array($var,get_object_vars($this)))
92 $this->$var=$value;
93 else {
94 $this->error(__FUNCTION__,$var." does not belong to PB_Page!");
95 }
96
97 }
98 /**
99 * 打开倒AJAX模式
100 *
101 * @param string $action 默认ajax触发的动作。
102 */
103 function open_ajax($action)
104 {
105 $this->is_ajax=true;
106 $this->ajax_action_name=$action;
107 }
108 /**
109 * 获取显示"下一页"的代码
110 *
111 * @param string $style
112 * @return string
113 */
114 function next_page($style='')
115 {
116 if($this->nowindex$this->totalpage){
117 return $this->_get_link($this->_get_url($this->nowindex+1),$this->next_page,$style);
118 }
119 return ''.$style.'">'.$this->next_page.'';
120 }
121
122 /**
123 * 获取显示“上一页”的代码
124 *
125 * @param string $style
126 * @return string
127 */
128 function pre_page($style='')
129 {
130 if($this->nowindex>1){
131 return $this->_get_link($this->_get_url($this->nowindex-1),$this->pre_page,$style);
132 }
133 return ''.$style.'">'.$this->pre_page.'';
134 }
135
136 /**
137 * 获取显示“首页”的代码
138 *
139 * @return string
140 */
141 function first_page($style='')
142 {
143 if($this->nowindex==1){
144 return ''.$style.'">'.$this->first_page.'';
145 }
146 return $this->_get_link($this->_get_url(1),$this->first_page,$style);
147 }
148
149 /**
150 * 获取显示“尾页”的代码
151 *
152 * @return string
153 */
154 function last_page($style='')
155 {
156 if($this->nowindex==$this->totalpage){
157 return ''.$style.'">'.$this->last_page.'';
158 }
159 return $this->_get_link($this->_get_url($this->totalpage),$this->last_page,$style);
160 }
161
162 function nowbar($style='',$nowindex_style='')
163 {
164 $plus=ceil($this->pagebarnum/2);
165 if($this->pagebarnum-$plus+$this->nowindex>$this->totalpage)$plus=($this->pagebarnum-$this->totalpage+$this->nowindex);
166
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31