当前位置:首页 > PHP教程 > php分页 > 列表

php 多风格的分页类(支持ajax分页)

发布:smiling 来源: PHP粉丝网  添加日期:2014-09-08 11:34:10 浏览: 评论:0 
  1. <?php 
  2. /** 
  3. * example: 
  4. * 模式四种分页模式: 
  5.    require_once('../libs/classes/page.class.php'); 
  6.    $page=new page(array('total'=>1000,'perpage'=>20)); 
  7.    echo 'mode:1<br>'.$page->show(); 
  8.    echo '<hr>mode:2<br>'.$page->show(2); 
  9.    echo '<hr>mode:3<br>'.$page->show(3); 
  10.    echo '<hr>mode:4<br>'.$page->show(4); 
  11.    开启AJAX: 
  12.    $ajaxpage=new page(array 
  13. ('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test')); 
  14.    echo 'mode:1<br>'.$ajaxpage->show(); 
  15.    采用继承自定义分页显示模式: 
  16. */ 
  17. class page 
  18. { 
  19. /** 
  20. * config ,public 
  21. */ 
  22. var $page_name="PB_page";//page标签,用来控制url页 
  23. var $next_page='>';//下一页 
  24. var $pre_page='<';//上一页 
  25. var $first_page='First';//首页 
  26. var $last_page='Last';//尾页 
  27. var $pre_bar='<<';//上一分页条 
  28. 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. //开源代码phpfensi.com 
  40. var $nowindex=1;//当前页 
  41. var $url="";//url地址头 
  42. var $offset=0; 
  43. /** 
  44. * constructor构造函数 
  45. * 
  46. * @param array $array['total'],$array['perpage'],$array['nowindex'],$array 
  47. ['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 
  53. of total'); 
  54.      $total=intval($array['total']); 
  55.      $perpage=(array_key_exists('perpage',$array))?intval($array['perpage']):10; 
  56.      $nowindex=(array_key_exists('nowindex',$array))?intval($array 
  57. ['nowindex']):''; 
  58.      $url=(array_key_exists('url',$array))?$array['url']:''; 
  59. }else{ 
  60.      $total=$array; 
  61.      $perpage=10; 
  62.      $nowindex=''; 
  63.      $url=''; 
  64. } 
  65. if((!is_int($total))||($total<0))$this->error(__FUNCTION__,$total.' is not a 
  66. positive integer!'); 
  67. if((!is_int($perpage))||($perpage<=0))$this->error(__FUNCTION__,$perpage.' is not 
  68. a positive integer!'); 
  69. if(!emptyempty($array['page_name']))$this->set('page_name',$array['page_name']);//设置 
  70. 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(!emptyempty($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- 
  111. >next_page,$style); 
  112. } 
  113. return '<span class=".$style.">'.$this->next_page.'</span>'; 
  114. } 
  115. /** 
  116. * 获取显示“上一页”的代码 
  117. * 
  118. * @param string $style 
  119. * @return string 
  120. */ 
  121. function pre_page($style='') 
  122. { 
  123. if($this->nowindex>1){ 
  124.    return $this->_get_link($this->_get_url($this->nowindex-1),$this- 
  125. >pre_page,$style); 
  126. } 
  127. return '<span class=".$style.">'.$this->pre_page.'</span>'; 
  128. } 
  129. /** 
  130. * 获取显示“首页”的代码 
  131. * 
  132. * @return string 
  133. */ 
  134. function first_page($style='') 
  135. { 
  136. if($this->nowindex==1){ 
  137.       return '<span class=".$style.">'.$this->first_page.'</span>'; 
  138. } 
  139. return $this->_get_link($this->_get_url(1),$this->first_page,$style); 
  140. } 
  141. /** 
  142. * 获取显示“尾页”的代码 
  143. * 
  144. * @return string 
  145. */ 
  146. function last_page($style='') 
  147. { 
  148. if($this->nowindex==$this->totalpage){ 
  149.       return '<span class=".$style.">'.$this->last_page.'</span>'; 
  150. } 
  151. return $this->_get_link($this->_get_url($this->totalpage),$this- 
  152. >last_page,$style); 
  153. } 
  154. function nowbar($style='',$nowindex_style='') 
  155. { 
  156. $plus=ceil($this->pagebarnum/2); 
  157. if($this->pagebarnum-$plus+$this->nowindex>$this->totalpage)$plus=($this- 
  158. >pagebarnum-$this->totalpage+$this->nowindex); 
  159. $begin=$this->nowindex-$plus+1; 
  160. $begin=($begin>=1)?$begin:1; 
  161. $return=''; 
  162. for($i=$begin;$i<$begin+$this->pagebarnum;$i++) 
  163. { 
  164.    if($i<=$this->totalpage){ 
  165.     if($i!=$this->nowindex) 
  166.         $return.=$this->_get_text($this->_get_link($this->_get_url 
  167. ($i),$i,$style)); 
  168.     else 
  169.         $return.=$this->_get_text('<span 
  170. class=".$nowindex_style.">'.$i.'</span>'); 
  171.    }else{ 
  172.     break; 
  173.    } 
  174.    $return.="n"; 
  175. } 
  176. unset($begin); 
  177. return $return; 
  178. } 
  179. /** 
  180. * 获取显示跳转按钮的代码 
  181. * 
  182. * @return string 
  183. */ 
  184. function select() 
  185. { 
  186.    $return='<select name="PB_Page_Select">'; 
  187. for($i=1;$i<=$this->totalpage;$i++) 
  188. { 
  189.    if($i==$this->nowindex){ 
  190.     $return.='<option value="'.$i.'" selected>'.$i.'</option>'; 
  191.    }else{ 
  192.     $return.='<option value="'.$i.'">'.$i.'</option>'; 
  193.    } 
  194. } 
  195. unset($i); 
  196. $return.='</select>'; 
  197. return $return; 
  198. } 
  199. /** 
  200. * 获取mysql教程 语句中limit需要的值 
  201. * 
  202. * @return string 
  203. */ 
  204. function offset() 
  205. { 
  206. return $this->offset; 
  207. } 
  208. /** 
  209. * 控制分页显示风格  
  210. * 
  211. * @param int $mode 
  212. * @return string 
  213. */ 
  214. function show($mode=1) 
  215. { 
  216. switch ($mode) 
  217. { 
  218.    case '1': 
  219.     $this->next_page='下一页'; 
  220.     $this->pre_page='上一页'; 
  221.     return $this->pre_page().$this->nowbar().$this->next_page().'第'.$this- 
  222. >select().'页'; 
  223.     break; 
  224.    case '2': 
  225.     $this->next_page='下一页'; 
  226.     $this->pre_page='上一页'; 
  227.     $this->first_page='首页'; 
  228.     $this->last_page='尾页'; 
  229.     return $this->first_page().$this->pre_page().'[第'.$this- 
  230. >nowindex.'页]'.$this->next_page().$this->last_page().'第'.$this->select().'页'; 
  231.     break; 
  232.    case '3': 
  233.     $this->next_page='下一页'; 
  234.     $this->pre_page='上一页'; 
  235.     $this->first_page='首页'; 
  236.     $this->last_page='尾页'; 
  237.     return $this->first_page().$this->pre_page().$this->next_page().$this- 
  238. >last_page(); 
  239.     break; 
  240.    case '4': 
  241.     $this->next_page='下一页'; 
  242.     $this->pre_page='上一页'; 
  243.     return $this->pre_page().$this->nowbar().$this->next_page(); 
  244.     break; 
  245.    case '5': 
  246.     return $this->pre_bar().$this->pre_page().$this->nowbar().$this->next_page 
  247. ().$this->next_bar(); 
  248.     break; 
  249. } 
  250. } 
  251. /*----------------private function (私有方法)----------------------------------- 
  252. ------------------------*/ 
  253. /** 
  254. * 设置url头地址 
  255. * @param: String $url 
  256. * @return boolean 
  257. */ 
  258. function _set_url($url="") 
  259. { 
  260. if(!emptyempty($url)){ 
  261.       //手动设置 
  262.    $this->url=$url.((stristr($url,'?'))?'&':'?').$this->page_name."="; 
  263. }else{ 
  264.       //自动获取 
  265.    if(emptyempty($_SERVER['QUERY_STRING'])){ 
  266.        //不存在QUERY_STRING时 
  267.     $this->url=$_SERVER['REQUEST_URI']."?".$this->page_name."="; 
  268.    }else{ 
  269.        // 
  270.     if(stristr($_SERVER['QUERY_STRING'],$this->page_name.'=')){ 
  271.         //地址存在页面参数 
  272.      $this->url=str_replace($this->page_name.'='.$this->nowindex,'',$_SERVER 
  273. ['REQUEST_URI']); 
  274.      $last=$this->url[strlen($this->url)-1]; 
  275.      if($last=='?'||$last=='&'){ 
  276.          $this->url.=$this->page_name."="; 
  277.      }else{ 
  278.          $this->url.='&'.$this->page_name."="; 
  279.      } 
  280.     }else{ 
  281.         // 
  282.      $this->url=$_SERVER['REQUEST_URI'].'&'.$this->page_name.'='; 
  283.     }//end if    
  284.    }//end if 
  285. }//end if 
  286. } 
  287. /** 
  288. * 设置当前页面 
  289. * 
  290. */ 
  291. function _set_nowindex($nowindex) 
  292. { 
  293. if(emptyempty($nowindex)){ 
  294.    //系统获取 
  295.    if(isset($_GET[$this->page_name])){ 
  296.     $this->nowindex=intval($_GET[$this->page_name]); 
  297.    } 
  298. }else{ 
  299.       //手动设置 
  300.    $this->nowindex=intval($nowindex); 
  301. } 
  302. } 
  303. /** 
  304. * 为指定的页面返回地址值 
  305. * 
  306. * @param int $pageno 
  307. * @return string $url 
  308. */ 
  309. function _get_url($pageno=1) 
  310. { 
  311. return $this->url.$pageno; 
  312. } 
  313. /** 
  314. * 获取分页显示文字,比如说默认情况下_get_text('<a href="">1</a>')将返回[<a 
  315. href="">1</a>] 
  316. * 
  317. * @param String $str 
  318. * @return string $url 
  319. */ 
  320. function _get_text($str) 
  321. { 
  322. return $this->format_left.$str.$this->format_right; 
  323. } 
  324. /** 
  325.    * 获取链接地址 
  326. */ 
  327. function _get_link($url,$text,$style=''){ 
  328. $style=(emptyempty($style))?'':'class="'.$style.'"'; 
  329. if($this->is_ajax){ 
  330.       //如果是使用AJAX模式 
  331.    return '<a '.$style.' href="网页特效:'.$this- 
  332. >ajax_action_name.'(''.$url.'')">'.$text.'</a>'; 
  333. }else{ 
  334.    return '<a '.$style.' href="'.$url.'">'.$text.'</a>'; 
  335. } 
  336. } 
  337. /** 
  338.    * 出错处理方式 
  339. */ 
  340. function error($function,$errormsg) 
  341. { 
  342.      die('Error in file <b>'.__FILE__.'</b> ,Function <b>'.$function.'()</b> 
  343. :'.$errormsg); 
  344. } 
  345. } 
  346. $page=new page(array('total'=>1000,'perpage'=>20)); 
  347. echo 'mode:1<br>'.$page->show(); 
  348. echo '<hr>mode:2<br>'.$page->show(2); 
  349. echo '<hr>mode:3<br>'.$page->show(3); 
  350. echo '<hr>mode:4<br>'.$page->show(4); 
  351. echo '<hr>开始AJAX模式:'; 
  352. $ajaxpage=new page(array 
  353. ('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test')); 
  354. echo 'mode:1<br>'.$ajaxpage->show(); 
  355. ?> 

Tags: php风格分页 php分页类

分享到: