欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

分享:一例PHP翻页(分页)类的实例代码

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