当前位置:Gxlcms > PHP教程 > 一个不错的php分页类的代码

一个不错的php分页类的代码

时间:2021-07-01 10:21:17 帮助过:6人阅读

  1. /**

  2. * filename: ext_page.class.php
  3. * @package:phpbean
  4. * @author :feifengxlq
  5. * @copyright :Copyright 2006 feifengxlq
  6. * @license:version 2.0
  7. * description: 分页类,四种分页模式
  8. * 2.0增加功能:支持自定义风格,自定义样式,同时支持PHP4和PHP5,
  9. * to see detail,please visit http://www.phpobject.net/blog/read.php?
  10. * example:
  11. * 模式四种分页模式:
  12. require_once('../libs/classes/page.class.php');
  13. $page=new page(array('total'=>1000,'perpage'=>20));
  14. echo 'mode:1
    '.$page->show();
  15. echo '
    mode:2
    '.$page->show(2);
  16. echo '
    mode:3
    '.$page->show(3);
  17. echo '
    mode:4
    '.$page->show(4);
  18. 开启AJAX:
  19. $ajaxpage=new page(array('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test'));
  20. echo 'mode:1
    '.$ajaxpage->show();
  21. 采用继承自定义分页显示模式:
  22. demo:http://www.phpobject.net/blog
  23. */
  24. class page
  25. {
  26. /**
  27. * config ,public
  28. */
  29. var $page_name="PB_page";//page标签,用来控制url页
  30. var $next_page='>';//下一页
  31. var $pre_page='<';//上一页
  32. var $first_page='First';//首页
  33. var $last_page='Last';//尾页
  34. var $pre_bar='<<';//上一分页条
  35. var $next_bar='>>';//下一分页条
  36. var $format_left='[';
  37. var $format_right=']';
  38. var $is_ajax=false;//是否支持AJAX分页模式

  39. /**

  40. * private
  41. *
  42. */
  43. var $pagebarnum=10;//控制记录条的个数。
  44. var $totalpage=0;//总页数
  45. var $ajax_action_name='';//AJAX动作名
  46. var $nowindex=1;//当前页
  47. var $url="";//url地址头
  48. var $offset=0;

  49. /**

  50. * constructor构造函数
  51. *
  52. * @param array $array['total'],$array['perpage'],$array['nowindex'],$array['url'],$array['ajax']
  53. */
  54. function page($array)
  55. {
  56. if(is_array($array)){
  57. if(!array_key_exists('total',$array))$this->error(__FUNCTION__,'need a param of total');
  58. $total=intval($array['total']);
  59. $perpage=(array_key_exists('perpage',$array))?intval($array['perpage']):10;
  60. $nowindex=(array_key_exists('nowindex',$array))?intval($array['nowindex']):'';
  61. $url=(array_key_exists('url',$array))?$array['url']:'';
  62. }else{
  63. $total=$array;
  64. $perpage=10;
  65. $nowindex='';
  66. $url='';
  67. }
  68. if((!is_int($total))||($total<0))$this->error(__FUNCTION__,$total.' is not a positive integer!');
  69. if((!is_int($perpage))||($perpage<=0))$this->error(__FUNCTION__,$perpage.' is not a positive integer!');
  70. if(!empty($array['page_name']))$this->set('page_name',$array['page_name']);//设置pagename
  71. $this->_set_nowindex($nowindex);//设置当前页
  72. $this->_set_url($url);//设置链接地址
  73. $this->totalpage=ceil($total/$perpage);
  74. $this->offset=($this->nowindex-1)*$perpage;
  75. if(!empty($array['ajax']))$this->open_ajax($array['ajax']);//打开AJAX模式
  76. }
  77. /**
  78. * 设定类中指定变量名的值,如果改变量不属于这个类,将throw一个exception
  79. *
  80. * @param string $var
  81. * @param string $value
  82. */
  83. function set($var,$value)
  84. {
  85. if(in_array($var,get_object_vars($this)))
  86. $this->$var=$value;
  87. else {
  88. $this->error(__FUNCTION__,$var." does not belong to PB_Page!");
  89. }

  90. }

  91. /**
  92. * 打开倒AJAX模式
  93. *
  94. * @param string $action 默认ajax触发的动作。
  95. */
  96. function open_ajax($action)
  97. {
  98. $this->is_ajax=true;
  99. $this->ajax_action_name=$action;
  100. }
  101. /**
  102. * 获取显示"下一页"的代码
  103. *
  104. * @param string $style
  105. * @return string
  106. */
  107. function next_page($style='')
  108. {
  109. if($this->nowindex<$this->totalpage){
  110. return $this->_get_link($this->_get_url($this->nowindex+1),$this->next_page,$style);
  111. }
  112. return ''.$this->next_page.'';
  113. }

  114. /**

  115. * 获取显示“上一页”的代码
  116. *
  117. * @param string $style
  118. * @return string
  119. */
  120. function pre_page($style='')
  121. {
  122. if($this->nowindex>1){
  123. return $this->_get_link($this->_get_url($this->nowindex-1),$this->pre_page,$style);
  124. }
  125. return ''.$this->pre_page.'';
  126. }

  127. /**

  128. * 获取显示“首页”的代码
  129. *
  130. * @return string
  131. */
  132. function first_page($style='')
  133. {
  134. if($this->nowindex==1){
  135. return ''.$this->first_page.'';
  136. }
  137. return $this->_get_link($this->_get_url(1),$this->first_page,$style);
  138. }

  139. /**

  140. * 获取显示“尾页”的代码
  141. *
  142. * @return string
  143. */
  144. function last_page($style='')
  145. {
  146. if($this->nowindex==$this->totalpage){
  147. return ''.$this->last_page.'';
  148. }
  149. return $this->_get_link($this->_get_url($this->totalpage),$this->last_page,$style);
  150. }

  151. function nowbar($style='',$nowindex_style='')

  152. {
  153. $plus=ceil($this->pagebarnum/2);
  154. if($this->pagebarnum-$plus+$this->nowindex>$this->totalpage)$plus=($this->pagebarnum-$this->totalpage+$this->nowindex);
  155. $begin=$this->nowindex-$plus+1;
  156. $begin=($begin>=1)?$begin:1;
  157. $return='';
  158. for($i=$begin;$i<$begin+$this->pagebarnum;$i++)
  159. {
  160. if($i<=$this->totalpage){
  161. if($i!=$this->nowindex)
  162. $return.=$this->_get_text($this->_get_link($this->_get_url($i),$i,$style));
  163. else
  164. $return.=$this->_get_text(''.$i.'');
  165. }else{
  166. break;
  167. }
  168. $return.="\n";
  169. }
  170. unset($begin);
  171. return $return;
  172. }
  173. /**
  174. * 获取显示跳转按钮的代码
  175. *
  176. * @return string
  177. */
  178. function select()
  179. {
  180. $return='';
  181. return $return;
  182. }

  183. /**

  184. * 获取mysql 语句中limit需要的值
  185. *
  186. * @return string
  187. */
  188. function offset()
  189. {
  190. return $this->offset;
  191. }

  192. /**

  193. * 控制分页显示风格
  194. *
  195. * @param int $mode
  196. * @return string
  197. */
  198. function show($mode=1)
  199. {
  200. switch ($mode)
  201. {
  202. case '1':
  203. $this->next_page='下一页';
  204. $this->pre_page='上一页';
  205. return $this->pre_page().$this->nowbar().$this->next_page().'第'.$this->select().'页';
  206. break;
  207. case '2':
  208. $this->next_page='下一页';
  209. $this->pre_page='上一页';
  210. $this->first_page='首页';
  211. $this->last_page='尾页';
  212. return $this->first_page().$this->pre_page().'[第'.$this->nowindex.'页]'.$this->next_page().$this->last_page().'第'.$this->select().'页';
  213. break;
  214. case '3':
  215. $this->next_page='下一页';
  216. $this->pre_page='上一页';
  217. $this->first_page='首页';
  218. $this->last_page='尾页';
  219. return $this->first_page().$this->pre_page().$this->next_page().$this->last_page();
  220. break;
  221. case '4':
  222. $this->next_page='下一页';
  223. $this->pre_page='上一页';
  224. return $this->pre_page().$this->nowbar().$this->next_page();
  225. break;
  226. case '5':
  227. return $this->pre_bar().$this->pre_page().$this->nowbar().$this->next_page().$this->next_bar();
  228. break;
  229. }

  230. }

  231. /*------private function (私有方法)---------*/
  232. /** @link:http://bbs.it-home.org
  233. * 设置url头地址
  234. * @param: String $url
  235. * @return boolean
  236. */
  237. function _set_url($url="")
  238. {
  239. if(!empty($url)){
  240. //手动设置
  241. $this->url=$url.((stristr($url,'?'))?'&':'?').$this->page_name."=";
  242. }else{
  243. //自动获取
  244. if(empty($_SERVER['QUERY_STRING'])){
  245. //不存在QUERY_STRING时
  246. $this->url=$_SERVER['REQUEST_URI']."?".$this->page_name."=";
  247. }else{
  248. //
  249. if(stristr($_SERVER['QUERY_STRING'],$this->page_name.'=')){
  250. //地址存在页面参数
  251. $this->url=str_replace($this->page_name.'='.$this->nowindex,'',$_SERVER['REQUEST_URI']);
  252. $last=$this->url[strlen($this->url)-1];
  253. if($last=='?'||$last=='&'){
  254. $this->url.=$this->page_name."=";
  255. }else{
  256. $this->url.='&'.$this->page_name."=";
  257. }
  258. }else{
  259. //
  260. $this->url=$_SERVER['REQUEST_URI'].'&'.$this->page_name.'=';
  261. }//end if
  262. }//end if
  263. }//end if
  264. }

  265. /**

  266. * 设置当前页面
  267. *
  268. */
  269. function _set_nowindex($nowindex)
  270. {
  271. if(empty($nowindex)){
  272. //系统获取

  273. if(isset($_GET[$this->page_name])){

  274. $this->nowindex=intval($_GET[$this->page_name]);
  275. }
  276. }else{
  277. //手动设置
  278. $this->nowindex=intval($nowindex);
  279. }
  280. }

  281. /**

  282. * 为指定的页面返回地址值
  283. *
  284. * @param int $pageno
  285. * @return string $url
  286. */
  287. function _get_url($pageno=1)
  288. {
  289. return $this->url.$pageno;
  290. }

  291. /**

  292. * 获取分页显示文字,比如说默认情况下_get_text('1')将返回[1]
  293. *
  294. * @param String $str
  295. * @return string $url
  296. */
  297. function _get_text($str)
  298. {
  299. return $this->format_left.$str.$this->format_right;
  300. }

  301. /**

  302. * 获取链接地址
  303. */
  304. function _get_link($url,$text,$style=''){
  305. $style=(empty($style))?'':'class="'.$style.'"';
  306. if($this->is_ajax){
  307. //如果是使用AJAX模式
  308. return 'ajax_action_name.'(\''.$url.'\')">'.$text.'';
  309. }else{
  310. return ''.$text.'';
  311. }
  312. }
  313. /**
  314. * 出错处理方式
  315. */
  316. function error($function,$errormsg)
  317. {
  318. die('Error in file '.__FILE__.' ,Function '.$function.'() :'.$errormsg);
  319. }
  320. }
  321. $page=new page(array('total'=>1000,'perpage'=>20));
  322. echo 'mode:1
    '.$page->show();
  323. echo '
    mode:2
    '.$page->show(2);
  324. echo '
    mode:3
    '.$page->show(3);
  325. echo '
    mode:4
    '.$page->show(4);
  326. echo '
    开始AJAX模式:';
  327. $ajaxpage=new page(array('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test'));
  328. echo 'mode:1
    '.$ajaxpage->show();
  329. ?>

人气教程排行