Rumah  >  Artikel  >  pembangunan bahagian belakang  >  php分页类调用实例教程

php分页类调用实例教程

WBOY
WBOYasal
2016-07-25 08:52:36740semak imbas
  1. /**

  2. * filename: ext_page.class.php
  3. * @package:phpbean
  4. * description:超强分页类,四种分页模式,默认采用类似baidu,google的分页风格。
  5. * 2.0增加功能:支持自定义风格,自定义样式,同时支持PHP4和PHP5,
  6. * example:
  7. * 模式四种分页模式:
  8. require_once('../libs/classes/page.class.php');
  9. $page=new page(array('total'=>1000,'perpage'=>20));
  10. echo 'mode:1
    '.$page->show();
  11. echo '
    mode:2
    '.$page->show(2);
  12. echo '
    mode:3
    '.$page->show(3);
  13. echo '
    mode:4
    '.$page->show(4);
  14. 开启AJAX:bbs.it-home.org
  15. $ajaxpage=new page(array('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test'));
  16. echo 'mode:1
    '.$ajaxpage->show();
  17. 采用继承自定义分页显示模式:
  18. */
  19. class Page
  20. {
  21. /**
  22. * config ,public
  23. */
  24. var $page_name="p";//page标签,用来控制url页。比如说xxx.php?PB_page=2中的PB_page
  25. var $next_page='>';//下一页
  26. var $pre_page=' var $first_page='First';//首页
  27. var $last_page='Last';//尾页
  28. var $pre_bar=' var $next_bar='>>';//下一分页条
  29. var $format_left='';
  30. var $format_right='';
  31. var $is_ajax=false;//是否支持AJAX分页模式
  32. /**

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

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

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

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

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

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

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

  177. * 获取mysql 语句中limit需要的值
  178. *
  179. * @return string
  180. */
  181. function offset()
  182. {
  183. return $this->offset;
  184. }
  185. /**

  186. * 控制分页显示风格(你可以增加相应的风格)
  187. *
  188. * @param int $mode
  189. * @return string
  190. */
  191. function show($mode=1)
  192. {
  193. switch ($mode)
  194. {
  195. case '1':
  196. $this->next_page='下一页';
  197. $this->pre_page='上一页';
  198. return $this->pre_page().$this->nowbar().$this->next_page().'第'.$this->select().'页';
  199. break;
  200. case '2':
  201. $this->next_page='下一页';
  202. $this->pre_page='上一页';
  203. $this->first_page='首页';
  204. $this->last_page='尾页';
  205. return $this->first_page().$this->pre_page().'[第'.$this->nowindex.'页]'.$this->next_page().$this->last_page().'第'.$this->select().'页';
  206. break;
  207. case '3':
  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->next_page().$this->last_page();
  213. break;
  214. case '4':
  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->nowbar().$this->next_page().$this->last_page();
  220. break;
  221. case '5':
  222. return $this->pre_bar().$this->pre_page().$this->nowbar().$this->next_page().$this->next_bar();
  223. break;
  224. }
  225. }

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

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

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

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

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

  297. * 获取链接地址
  298. */
  299. function _get_link($url,$text,$style=''){
  300. $style=(empty($style))?'':'class="'.$style.'"';
  301. if($this->is_ajax){
  302. //如果是使用AJAX模式
  303. return ''.$text.'';
  304. }else{
  305. return ''.$text.'';
  306. }
  307. }
  308. /**
  309. * 出错处理方式
  310. */
  311. function error($function,$errormsg)
  312. {
  313. die('Error in file '.__FILE__.' ,Function '.$function.'() :'.$errormsg);
  314. }
  315. }
  316. ?>
复制代码


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn