Heim > Backend-Entwicklung > PHP-Tutorial > 一个不错的php分页类的代码

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

WBOY
Freigeben: 2016-07-25 09:05:18
Original
806 Leute haben es durchsucht
  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='var $first_page='First';//首页
  32. var $last_page='Last';//尾页
  33. var $pre_bar='var $next_bar='>>';//下一分页条
  34. var $format_left='[';
  35. var $format_right=']';
  36. var $is_ajax=false;//是否支持AJAX分页模式
  37. /**

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

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

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

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

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

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

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

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

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

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

  264. * 设置当前页面
  265. *
  266. */
  267. function _set_nowindex($nowindex)
  268. {
  269. if(empty($nowindex)){
  270. //系统获取
  271. if(isset($_GET[$this->page_name])){

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

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

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

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


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage