php 经典的分页代码类

这是一款php分页代码是一款经典的可自动判断url参数再进行分页的实例代码,同时自定了上页,下页,首页,尾页,同时还支持select下拉框跳转代码.

php 经典的分页代码类代码如下:

  1. class page{
  2. var $page_name="page";
  3. var $next_page='下一页';
  4. var $pre_page='上一页';
  5. var $first_page='首页';
  6. var $last_page='尾页';
  7. var $pre_bar='<<';
  8. var $next_bar='>>';
  9. var $format_left='';
  10. var $format_right='';
  11. var $pagebarnum=5;
  12. var $totalpage=0;
  13. var $nowindex=1;
  14. var $url="";
  15. var $offset=0;
  16. var $rewrite = array();
  17. function page($array)
  18. {
  19. if(is_array($array)){
  20. if(!array_key_exists('total',$array))$this->error(__function__,'need a param of total');
  21. $total=intval($array['total']);
  22. $perpage=(array_key_exists('perpage',$array))?intval($array['perpage']):10;
  23. $nowindex=(array_key_exists('nowindex',$array))?intval($array['nowindex']):'';
  24. $url=(array_key_exists('url',$array))?$array['url']:'';
  25. $action = (array_key_exists('action', $array)) ? $array['action'] : '';
  26. $id0 = (array_key_exists('id0', $array)) ? $array['id0'] : '';
  27. $id1 = (array_key_exists('id1', $array)) ? $array['id1'] : '';
  28. $id2 = (array_key_exists('id2', $array)) ? $array['id2'] : '';
  29. $id3 = (array_key_exists('id3', $array)) ? $array['id3'] : '';
  30. }else{
  31. $total=$array;
  32. $perpage=10;
  33. $nowindex='';
  34. $url='';
  35. $action = '';
  36. $id0 = '';
  37. $id1 = '';
  38. $id2 = '';
  39. $id3 = '';
  40. }
  41. if((!is_int($total))||($total<0))$this->error(__function__,$total.' is not a positive integer!');
  42. if((!is_int($perpage))||($perpage<=0))$this->error(__function__,$perpage.' is not a positive integer!');
  43. if(!emptyempty($array['page_name']))$this->set('page_name',$array['page_name']);
  44. $this->_set_nowindex($nowindex);
  45. $this->_set_url($url);
  46. $this->totalpage=ceil($total/$perpage);
  47. $this->offset=($this->nowindex-1)*$perpage;
  48. $this->action = $action;
  49. $this->rewrite = array('action'=>$action,'id0'=>$id0, 'id1'=>$id1, 'id2'=>$id2, 'id3'=>$id3);
  50. }
  51. function set($var,$value)
  52. {
  53. if(in_array($var,get_object_vars($this)))
  54. $this->$var=$value;
  55. else {
  56. $this->error(__function__,$var." does not belong to pb_page!");
  57. }
  58. }
  59. function next_page($){
  60. if($this->nowindex<$this->totalpage){
  61. return $this->_get_link($this->_get_url($this->nowindex+1),$this->next_page,$style);
  62. }
  63. return '<a class="'.$style.'">'.$this->next_page.'</a>';
  64. }
  65. function pre_page($){
  66. if($this->nowindex>1){
  67. return $this->_get_link($this->_get_url($this->nowindex-1),$this->pre_page,$style);
  68. }
  69. return '<a class="'.$style.'">'.$this->pre_page.'</a>';
  70. }
  71. function first_page($){
  72. if($this->nowindex==1){
  73. return '<a class="'.$style.'">'.$this->first_page.'</a>';
  74. }
  75. return $this->_get_link($this->_get_url(1),$this->first_page,$style);
  76. }
  77. function last_page($){
  78. if($this->nowindex==$this->totalpage||$this->totalpage==0){
  79. return '<a class="'.$style.'">'.$this->last_page.'</a>';
  80. }
  81. return $this->_get_link($this->_get_url($this->totalpage),$this->last_page,$style);
  82. }
  83. function nowbar($,$nowindex_)
  84. {
  85. $plus=ceil($this->pagebarnum/2);
  86. if($this->pagebarnum-$plus+$this->nowindex>$this->totalpage)$plus=($this->pagebarnum-$this->totalpage+$this->nowindex);
  87. $begin=$this->nowindex-$plus+1;
  88. $begin=($begin>=1)?$begin:1;
  89. $return='';
  90. for($i=$begin;$i<$begin+$this->pagebarnum;$i++)
  91. {
  92. if($i<=$this->totalpage){
  93. if($i!=$this->nowindex)
  94. $return.=$this->_get_text($this->_get_link($this->_get_url($i),$i,$style));
  95. else
  96. $return.=$this->_get_text('<a class="'.$nowindex_style.'">'.$i.'</a>');
  97. }else{
  98. break;
  99. }
  100. $return.=" ";
  101. }
  102. unset($begin);
  103. return $return;
  104. }
  105. /**
  106. * 获取显示跳转按钮的代码
  107. *
  108. * @return string
  109. */
  110. function select()
  111. {
  112. $return='<select name="pb_page_select">';
  113. for($i=1;$i<=$this->totalpage;$i++)
  114. {
  115. if($i==$this->nowindex){
  116. $return.='<option value="'.$i.'" selected>'.$i.'</option>';
  117. }else{
  118. $return.='<option value="'.$i.'">'.$i.'</option>';
  119. }
  120. }
  121. unset($i);
  122. $return.='</select>';
  123. return $return;
  124. }
  125. /**
  126. * 获取mysql教程 语句中limit需要的值
  127. *
  128. * @return string
  129. */
  130. function offset()
  131. {
  132. return $this->offset;
  133. }
  134. /**
  135. * 控制分页显示风格(你可以增加相应的风格)
  136. *
  137. * @param int $mode
  138. * @return string
  139. */
  140. function show($mode=1)
  141. {
  142. switch ($mode)
  143. {
  144. case '1':
  145. $this->next_page='下一页';
  146. $this->pre_page='上一页';
  147. return $this->pre_page().$this->nowbar().$this->next_page().'第'.$this->select().'页';
  148. break;
  149. case '2':
  150. $this->next_page='下一页';
  151. $this->pre_page='上一页';
  152. $this->first_page='首页';
  153. $this->last_page='尾页';
  154. return $this->first_page().$this->pre_page().'[第'.$this->nowindex.'页]'.$this->next_page().$this->last_page().'第'.$this->select().'页';
  155. break;
  156. case '3':
  157. $this->next_page='下一页';
  158. $this->pre_page='上一页';
  159. $this->first_page='首页';
  160. $this->last_page='尾页';
  161. return $this->first_page("page_box")."".$this->pre_page("page_box")."".$this->nowbar("page_box_a","page_box_b")."".$this->next_page("page_box")."".$this->last_page("page_box")."<a class="clear"></a>";
  162. break;
  163. case '4':
  164. $this->next_page='下一页';
  165. $this->pre_page='上一页';
  166. return $this->pre_page().$this->nowbar().$this->next_page();
  167. break;
  168. case '5':
  169. return $this->pre_bar().$this->pre_page().$this->nowbar().$this->next_page().$this->next_bar();
  170. break;
  171. }
  172. }
  173. function _set_url($url="")
  174. {
  175. if(!emptyempty($url)){
  176. $this->url=$url.((stristr($url,'?'))?'&':'?').$this->page_name."=";
  177. }else{
  178. if(emptyempty($_server['query_string'])){
  179. $this->url=$_server['request_uri']."?".$this->page_name."=";
  180. }else{
  181. if(stristr($_server['query_string'],$this->page_name.'=')){
  182. $this->url=str_replace($this->page_name.'='.$this->nowindex,'',$_server['request_uri']);
  183. $last=$this->url[strlen($this->url)-1];
  184. if($last=='?'||$last=='&'){
  185. $this->url.=$this->page_name."=";
  186. }else{
  187. $this->url.='&'.$this->page_name."=";
  188. }
  189. }else{
  190. $this->url=$_server['request_uri'].'&'.$this->page_name.'=';
  191. }
  192. }
  193. }
  194. }
  195. function _set_nowindex($nowindex)
  196. {
  197. if(emptyempty($nowindex)){
  198. if(isset($_get[$this->page_name])){
  199. $this->nowindex=intval($_get[$this->page_name]);
  200. }
  201. }else{
  202. $this->nowindex=intval($nowindex);
  203. }
  204. }
  205. function _get_url($pageno=1)
  206. {
  207. global $_cfg;
  208. $arr = $this->rewrite;
  209. //print_r($arr);
  210. //print_r($this->url.$pageno);
  211. if($_cfg['urlrewrite'] && !emptyempty($arr['action'])){
  212. return url_rewrite($arr['action'], array('id0'=>$arr['id0'],'id1'=>$arr['id1'],'id2'=>$arr['id2'],'id3'=>$arr['id3'],'page'=>$pageno));
  213. }else{
  214. return $this->url.$pageno;
  215. }
  216. }
  217. function _get_text($str)
  218. {
  219. return $this->format_left.$str.$this->format_right;
  220. }
  221. function _get_link($url,$text,$){
  222. $':'class="'.$style.'"';
  223. return '<a '.$style.' href="'.$url.'">'.$text.'</a>';
  224. }
  225. function error($function,$errormsg)
  226. //开源代码phpfensi.com
  227. {
  228. die('error in file <b>'.__file__.'</b> ,function <b>'.$function.'()</b> :'.$errormsg);
  229. }
  230. }