当前位置:Gxlcms > PHP教程 > php分页类调用实例教程

php分页类调用实例教程

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

  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='<';//上一页
  27. var $first_page='First';//首页
  28. var $last_page='Last';//尾页
  29. var $pre_bar='<<';//上一分页条
  30. var $next_bar='>>';//下一分页条
  31. var $format_left='';
  32. var $format_right='';
  33. var $is_ajax=false;//是否支持AJAX分页模式

  34. /**

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

  44. /**

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

  85. }

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

  109. /**

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

  122. /**

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

  134. /**

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

  146. function nowbar($style='',$nowindex_style='c')

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

  178. /**

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

  187. /**

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

  227. }

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

  262. /**

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

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

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

  278. /**

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

  288. /**

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

  298. /**

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

人气教程排行