登录  /  注册
分享:一例PHP翻页(分页)类的实例代码
php中文网
发布: 2016-07-25 08:52:25
原创
692人浏览过
  1. /**
  2. * filename: ext_page.class.php
  3. * @package:phpbean
  4. * descrīption:超强分页类,四种分页模式,默认采用类似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:
  15. $ajaxpage=new page(array('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test'));
  16. echo 'mode:1
    '.$ajaxpage->show();
  17. 采用继承自定义分页显示模式。
  18. 编辑整理:脚本学堂 http://bbs.it-home.org
  19. */
  20. class _page
  21. {
  22. /**
  23. * config ,public
  24. */
  25. var $page_name="PB_page";//page标签,用来控制url页。比如说xxx.php?PB_page=2中的PB_page
  26. var $next_page='>';//下一页
  27. var $pre_page='<';//上一页
  28. var $first_page='First';//首页
  29. var $last_page='Last';//尾页
  30. var $pre_bar='<<';//上一分页条
  31. var $next_bar='>>';//下一分页条
  32. var $format_left='[';
  33. var $format_right=']';
  34. var $is_ajax=false;//是否支持AJAX分页模式
  35. /**
  36. * private
  37. *
  38. */
  39. var $pagebarnum=10;//控制记录条的个数。
  40. var $totalpage=0;//总页数
  41. var $ajax_action_name='';//AJAX动作名
  42. var $nowindex=1;//当前页
  43. var $url="";//url地址头
  44. var $offset=0;
  45. /**
  46. * constructor构造函数
  47. *
  48. * @param array $array['total'],$array['perpage'],$array['nowindex'],$array['url'],$array['ajax']...
  49. */
  50. function page($array)
  51. {
  52. if(is_array($array)){
  53. if(!array_key_exists('total',$array))$this->error(__FUNCTION__,'need a param 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['nowindex']):'';
  57. $url=(array_key_exists('url',$array))?$array['url']:'';
  58. }else{
  59. $total=$array;
  60. $perpage=10;
  61. $nowindex='';
  62. $url='';
  63. }
  64. if((!is_int($total))||($total<0))$this->error(__FUNCTION__,$total.' is not a positive integer!');
  65. if((!is_int($perpage))||($perpage<=0))$this->error(__FUNCTION__,$perpage.' is not a positive integer!');
  66. if(!empty($array['page_name']))$this->set('page_name',$array['page_name']);//设置pagename
  67. $this->_set_nowindex($nowindex);//设置当前页
  68. $this->_set_url($url);//设置链接地址
  69. $this->totalpage=ceil($total/$perpage);
  70. $this->offset=($this->nowindex-1)*$perpage;
  71. if(!empty($array['ajax']))$this->open_ajax($array['ajax']);//打开AJAX模式
  72. }
  73. /**
  74. * 设定类中指定变量名的值,如果改变量不属于这个类,将throw一个exception
  75. *
  76. * @param string $var
  77. * @param string $value
  78. */
  79. function set($var,$value)
  80. {
  81. if(in_array($var,get_object_vars($this)))
  82. $this->$var=$value;
  83. else {
  84. $this->error(__FUNCTION__,$var." does not belong to PB_Page!");
  85. }
  86. }
  87. /**
  88. * 打开倒AJAX模式
  89. *
  90. * @param string $action 默认ajax触发的动作。
  91. */
  92. function open_ajax($action)
  93. {
  94. $this->is_ajax=true;
  95. $this->ajax_action_name=$action;
  96. }
  97. /**
  98. * 获取显示"下一页"的代码
  99. *
  100. * @param string $style
  101. * @return string
  102. */
  103. function next_page($style='')
  104. {
  105. if($this->nowindex<$this->totalpage){
  106. return $this->_get_link($this->_get_url($this->nowindex+1),$this->next_page,$style);
  107. }
  108. return ''.$this->next_page.'';
  109. }
  110. /**
  111. * 获取显示“上一页”的代码
  112. *
  113. * @param string $style
  114. * @return string
  115. */
  116. function pre_page($style='')
  117. {
  118. if($this->nowindex>1){
  119. return $this->_get_link($this->_get_url($this->nowindex-1),$this->pre_page,$style);
  120. }
  121. return ''.$this->pre_page.'';
  122. }
  123. /**
  124. * 获取显示“首页”的代码
  125. *
  126. * @return string
  127. */
  128. function first_page($style='')
  129. {
  130. if($this->nowindex==1){
  131. return ''.$this->first_page.'';
  132. }
  133. return $this->_get_link($this->_get_url(1),$this->first_page,$style);
  134. }
  135. /**
  136. * 获取显示“尾页”的代码
  137. *
  138. * @return string
  139. */
  140. function last_page($style='')
  141. {
  142. if($this->nowindex==$this->totalpage){
  143. return ''.$this->last_page.'';
  144. }
  145. return $this->_get_link($this->_get_url($this->totalpage),$this->last_page,$style);
  146. }
  147. function nowbar($style='',$nowindex_style='')
  148. {
  149. $plus=ceil($this->pagebarnum/2);
  150. if($this->pagebarnum-$plus+$this->nowindex>$this->totalpage)$plus=($this->pagebarnum-$this->totalpage+$this->nowindex);
  151. $begin=$this->nowindex-$plus+1;
  152. $begin=($begin>=1)?$begin:1;
  153. $return='';
  154. for($i=$begin;$i<$begin+$this->pagebarnum;$i++)
  155. {
  156. if($i<=$this->totalpage){
  157. if($i!=$this->nowindex)
  158. $return.=$this->_get_text($this->_get_link($this->_get_url($i),$i,$style));
  159. else
  160. $return.=$this->_get_text(''.$i.'');
  161. }else{
  162. break;
  163. }
  164. $return.="\n";
  165. }
  166. unset($begin);
  167. return $return;
  168. }
  169. /**
  170. * 获取显示跳转按钮的代码
  171. *
  172. * @return string
  173. */
  174. function select()
  175. {
  176. $return='';
  177. return $return;
  178. }
  179. /**
  180. * 获取mysql 语句中limit需要的值
  181. *
  182. * @return string
  183. */
  184. function offset()
  185. {
  186. return $this->offset;
  187. }
  188. /**
  189. * 控制分页显示风格(你可以增加相应的风格)
  190. *
  191. * @param int $mode
  192. * @return string
  193. */
  194. function show($mode=1)
  195. {
  196. switch ($mode)
  197. {
  198. case '1':
  199. $this->next_page='下一页';
  200. $this->pre_page='上一页';
  201. return $this->pre_page().$this->nowbar().$this->next_page().'第'.$this->select().'页';
  202. break;
  203. case '2':
  204. $this->next_page='下一页';
  205. $this->pre_page='上一页';
  206. $this->first_page='首页';
  207. $this->last_page='尾页';
  208. return $this->first_page().$this->pre_page().'[第'.$this->nowindex.'页]'.$this->next_page().$this->last_page().'第'.$this->select().'页';
  209. break;
  210. case '3':
  211. $this->next_page='下一页';
  212. $this->pre_page='上一页';
  213. $this->first_page='首页';
  214. $this->last_page='尾页';
  215. return $this->first_page().$this->pre_page().$this->next_page().$this->last_page();
  216. break;
  217. case '4':
  218. $this->next_page='下一页';
  219. $this->pre_page='上一页';
  220. return $this->pre_page().$this->nowbar().$this->next_page();
  221. break;
  222. case '5':
  223. return $this->pre_bar().$this->pre_page().$this->nowbar().$this->next_page().$this->next_bar();
  224. break;
  225. case '6':
  226. return $this->select();
  227. break;
  228. case '7':
  229. return $this->nowbar();
  230. break;
  231. }
  232. }
  233. /*----------------private function (私有方法)-----------------------------------------------------------*/
  234. /**
  235. * 设置url头地址
  236. * @param: String $url
  237. * @return boolean
  238. */
  239. function _set_url($url="")
  240. {
  241. if(!empty($url)){
  242. //手动设置
  243. $this->url=$url.((stristr($url,'?'))?'&':'?').$this->page_name."=";
  244. }else{
  245. //自动获取
  246. if(empty($_SERVER['QUERY_STRING'])){
  247. //不存在QUERY_STRING时
  248. $this->url=$_SERVER['REQUEST_URI']."?".$this->page_name."=";
  249. }else{
  250. //
  251. if(stristr($_SERVER['QUERY_STRING'],$this->page_name.'=')){
  252. //地址存在页面参数
  253. $this->url=str_replace($this->page_name.'='.$this->nowindex,'',$_SERVER['REQUEST_URI']);
  254. $last=$this->url[strlen($this->url)-1];
  255. if($last=='?'||$last=='&'){
  256. $this->url.=$this->page_name."=";
  257. }else{
  258. $this->url.='&'.$this->page_name."=";
  259. }
  260. }else{
  261. //
  262. $this->url=$_SERVER['REQUEST_URI'].'&'.$this->page_name.'=';
  263. }//end if
  264. }//end if
  265. }//end if
  266. }
  267. /**
  268. * 设置当前页面
  269. *
  270. */
  271. function _set_nowindex($nowindex)
  272. {
  273. if(empty($nowindex)){
  274. //系统获取
  275. if(isset($_GET[$this->page_name])){
  276. $this->nowindex=intval($_GET[$this->page_name]);
  277. }
  278. if(isset($_POST['PB_Page_Select'])){
  279. $this->nowindex=$_POST['PB_Page_Select'];
  280. }
  281. }else{
  282. //手动设置
  283. $this->nowindex=intval($nowindex);
  284. }
  285. }
  286. /**
  287. * 为指定的页面返回地址值
  288. *
  289. * @param int $pageno
  290. * @return string $url
  291. */
  292. function _get_url($pageno=1)
  293. {
  294. return $this->url.$pageno;
  295. }
  296. /**
  297. * 获取分页显示文字,比如说默认情况下_get_text('1')将返回[1]
  298. *
  299. * @param String $str
  300. * @return string $url
  301. */
  302. function _get_text($str)
  303. {
  304. return $this->format_left.$str.$this->format_right;
  305. }
  306. /**
  307. * 获取链接地址
  308. */
  309. function _get_link($url,$text,$style=''){
  310. $style=(empty($style))?'':'class="'.$style.'"';
  311. if($this->is_ajax){
  312. //如果是使用AJAX模式
  313. return ''.$text.'';
  314. }else{
  315. return ''.$text.'';
  316. }
  317. }
  318. /**
  319. * 出错处理方式
  320. */
  321. function error($function,$errormsg)
  322. {
  323. die('Error in file '.__FILE__.' ,Function '.$function.'() :'.$errormsg);
  324. }
  325. }
  326. // 继承分页类,加入数据库访问能力.
  327. class Page extends _Page {
  328. var $db; //db connected object
  329. var $_Sql_Query = ''; //查询数据库的sql
  330. var $_Total = 0; //查询到的总记录.必须先是
  331. var $_Rst = array(); //查询到的记录.
  332. /**
  333. * 分页查询类库.
  334. *
  335. * @param String $Sql 记录查询的SQL 语句.
  336. * @param int $pagenuber 每页多少条记录.
  337. * @param int $pagen 当前页面.
  338. * @param String $url 分页链接带入的参数. index.php?xx=b&bb=33
  339. * @param String $pname 当前第几页的标记,默认是 index.php?xx=b&bb=33&page=2 如果有特殊要求
  340. 可以修改 $pname的参数. 比如: $pname='db_page',则变成: index.php?xx=b&bb=33&db_page=2
  341. * @return Mysql_Page
  342. */
  343. function Page($db, $sql_query = '', $max_rows_per_page = 20, $current_page_number = 0, $url = '', $parameters = '', $pname = 'PB_page',$otc = '*') {
  344. $this -> db = $db;
  345. $pos_to = strlen($sql_query);
  346. $pos_from = strpos($sql_query, ' from', 0);
  347. $pos_group_by = strpos($sql_query, ' group by', $pos_from);
  348. if (($pos_group_by < $pos_to) && ($pos_group_by != false)) $pos_to = $pos_group_by;
  349. $pos_having = strpos($sql_query, ' having', $pos_from);
  350. if (($pos_having < $pos_to) && ($pos_having != false)) $pos_to = $pos_having;
  351. $pos_order_by = strpos($sql_query, ' order by', $pos_from);
  352. if (($pos_order_by < $pos_to) && ($pos_order_by != false)) $pos_to = $pos_order_by;
  353. $reviews_count = $this -> db -> getResults("select count($otc) as total " . substr($sql_query, $pos_from, ($pos_to - $pos_from)));
  354. $query_num_rows = $reviews_count[0]['total'];
  355. $this -> _Total = $query_num_rows;
  356. $num_pages = ceil($query_num_rows / $max_rows_per_page);
  357. if ($current_page_number > $num_pages) {
  358. $current_page_number = $num_pages;
  359. }
  360. $offset = ($max_rows_per_page * ($current_page_number - 1));
  361. if ($offset < 0) $offset = 0;
  362. if ($offset > 0) {
  363. $offset = $offset + 1;
  364. }
  365. $this -> _Sql_Query = $sql_query . " limit " . $offset . ", " . $max_rows_per_page;
  366. $this -> setData(); //查询数据库.
  367. parent :: page(array('total' => $query_num_rows, 'perpage' => $max_rows_per_page, 'page_name' => $pname, 'url' => $url, 'parameters' => $parameters));
  368. }
  369. /**
  370. * 取得当前页面的记录,返回一个数组.
  371. */
  372. function findByAll() {
  373. return $this -> _Rst;
  374. }
  375. /**
  376. * 显示分页信息.
  377. *
  378. * @param int $model
  379. */
  380. function dispaly_links($model) {
  381. $this -> show($model);
  382. }
  383. /**
  384. * 返回记录数.
  385. *
  386. * @return Int
  387. */
  388. function getCount() {
  389. return $this -> _Total;
  390. }
  391. /**
  392. * 取查询结果记录数..
  393. *
  394. * @return Int
  395. */
  396. function getRows() {
  397. return count($this -> _Rst);
  398. }
  399. /**
  400. * 执行查询功能.
  401. * 计算数组.
  402. * 私有方法.
  403. */
  404. function setData() {
  405. $this -> _Rst = $this -> db -> getResults($this -> _Sql_Query);
  406. }
  407. }
  408. ?>
复制代码


来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学