免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2181 | 回复: 6
打印 上一主题 下一主题

php5实现的数据分页类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-05-15 00:08 |只看该作者 |倒序浏览
本类是把数组中的数据分页显示出来,
如要显示数据库中的数据,先把数据库中的数据倒入数组。
本人学php才几个月,请大家多批评一下。

下面是分页类和把数据库中数据倒入数组的类源码。

分页类:
$forStartCtrl 和 $forEndCtrl 两个变量是在显示数据时用来控制for循环.


  1. <?php
  2. /***************************************************

  3.     [UPLINK_SKY] index.php  --Meng LinuxRuby.
  4.         This is the freeware.

  5.         Version  : 0.1.
  6.         Author   : Meng LinuxRuby (sleep_linux@163.com).
  7.         CopyRight: (C)2004.05.* Meng LinuxRuby.

  8. ***************************************************/


  9. class Page{
  10.         private $countOfPage;
  11.         private $isThisPage;
  12.         private $isFirstPage;
  13.         private $isEndPage;
  14.         private $prevPage;
  15.         private $nextPage;
  16.         private $numOfData;
  17.         private $pageSize = 10;
  18.         private $forStartCtrl;
  19.         private $forEndCtrl;

  20.         function __construct($dataArray){
  21.                 if (!empty($_GET['page'])){
  22.                         $this->;isThisPage = $_GET['page'];
  23.                 }else{
  24.                         $this->;isThisPage = 1;
  25.                 }
  26.                 if (!empty($_GET['pageSize']) || !empty($_POST['pageSize'])){
  27.                         if (!empty($_GET['pageSize'])){
  28.                                 $this->;pageSize = $_GET['pageSize'];
  29.                         }elseif(!empty($_POST['pageSize'])){
  30.                                 $this->;pageSize = $_POST['pageSize'];
  31.                         }
  32.                 }
  33.                 try{
  34.                         $this->;numOfData = count($dataArray);
  35.                         if ($this->;numOfData >; 0){
  36.                             $this->;countOfPage = ceil($this->;numOfData / $this->;pageSize);
  37.                         }else{
  38.                                 $this->;countOfPage = 1;
  39.                         }
  40.                 }catch(Exception $exception){
  41.                         print $exception->;getMessage();
  42.                 }
  43.                 if ($this->;countOfPage == 1){
  44.                         $this->;isFirstPage = true;
  45.                         $this->;isEndPage = true;
  46.                 }elseif ($this->;isThisPage == 1){
  47.                         $this->;isFirstPage = true;
  48.                         $this->;isEndPage = false;
  49.                 }elseif ($this->;isThisPage == $this->;countOfPage){
  50.                         $this->;isFirstPage = false;
  51.                     $this->;isEndPage = true;
  52.                 }else{
  53.                         $this->;isFirstPage = false;
  54.                         $this->;isEndPage = false;
  55.                 }
  56.                 if ($this->;countOfPage >; 1){
  57.                         if (!$this->;isFirstPage){
  58.                                 $this->;prevPage = $this->;isThisPage - 1;
  59.                         }
  60.                         if (!$this->;isEndPage){
  61.                                 $this->;nextPage = $this->;isThisPage + 1;
  62.                         }
  63.                 }
  64.                 $this->;forStartCtrl = ($this->;isThisPage - 1) * $this->;pageSize;
  65.                 if (($this->;isThisPage * $this->;pageSize) >; $this->;numOfData){
  66.                         $this->;forEndCtrl = $this->;numOfData;
  67.                 }else{
  68.                         $this->;forEndCtrl = $this->;isThisPage * $this->;pageSize;
  69.                 }
  70.                 return true;
  71.         }

  72.         function getStartCtrl(){
  73.                 return $this->;forStartCtrl;
  74.         }

  75.         function getEndCtrl(){
  76.                 return $this->;forEndCtrl;
  77.         }

  78.         function showPageTag(){
  79.                 if ($this->;isFirstPage){
  80.                         $tag = "首页|上一页|";
  81.                 }else{
  82.                         $tag = "<a href='?page=1'>;首页</a>;|<a href='?page=".$this->;prevPage."'>;上一页</a>;|";
  83.                 }
  84.                 if ( $this->;isEndPage ){
  85.             $tag .= "下一页|尾页";
  86.         }else{
  87.             $tag .= "<a href='?page=".$this->;nextPage."'>;下一页</a>;|<a href='?page=".$this->;countOfPage."'>;尾页</a>;";
  88.                 }
  89.                 print $tag." 当前第".$this->;isThisPage."页 总共".$this->;countOfPage."页";
  90.         }

  91. }

  92. ?>;

复制代码


把MySQL数据库数据倒入数组类,如用别的数据库,请修改相关代码。

  1. <?php
  2. /***************************************************

  3.                 [UPLINK_SKY] mysql.class.php  --Meng LinuxRuby.
  4.         This is the freeware.

  5.         Author   : Meng LinuxRuby (sleep_linux@163.com).
  6.         CopyRight: (C)2004.05.* Meng LinuxRuby.

  7. ***************************************************/

  8. class mysqlDB {
  9.         private $sqlResult = array();
  10.         private $sqlLink;

  11.         function __construct($dbHost, $dbUser, $dbPass, $dbBase){
  12.                 try{
  13.                         $this->;sqlLink = MYSQL_CONNECT($dbHost, $dbUser, $dbPass);
  14.                         MYSQL_SELECT_DB($dbBase);
  15.                 }catch(Exception $exception){
  16.                         print "Can't Connect to MySQL Database";
  17.                         print $exception->;getMessage();
  18.                 }
  19.         }

  20.         function fetchArray($sqlString){
  21.                 try{
  22.                     if ($Rs = MYSQL_QUERY($sqlString, $this->;sqlLink)){
  23.                             while ($result = MYSQL_FETCH_ARRAY($Rs)){
  24.                                     $this->;sqlResult[] = $result;
  25.                             }
  26.                             return $this->;sqlResult;
  27.                     }else{
  28.                             return false;
  29.                         }
  30.                 }catch(Exception $exception){
  31.                         print $exception->;getMessage();
  32.                 }
  33.         }


  34.         function __destruct(){
  35.                 try{
  36.                         return MYSQL_CLOSE($this->;sqlLink) ? TRUE:FALSE;
  37.                 }catch(Exception $exception){
  38.                         print $exception->;getMessage();
  39.                 }
  40.         }
  41. }

  42. ?>;

复制代码

使用范例:


  1. $page = new Page($dataArray);
  2. for ($i=$page->;getStartCtrl();$i<$page->;getEndCtrl();$i++){
  3.     //显示数据
  4.     echo "<br>;".$dataArray[$i]['User_ID'];
  5.     echo "<br>;".$dataArray[$i]['User_Pass'];
  6.     echo "<br>;".$dataArray[$i]['User_Auth'];
  7. }
  8. //页标
  9. print $page->;showPageTag();

复制代码

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2004-05-15 00:19 |只看该作者

php5实现的数据分页类

建议以后发代码用[code][/code]把代码框起来,这样其他人比较容易浏览

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
3 [报告]
发表于 2004-05-15 00:43 |只看该作者

php5实现的数据分页类

前赴后继!
看来PHP朝全面面向OO进军了。呵呵,大家都在搞,形势一片大好,全国山河一片红啊。
不错。

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
4 [报告]
发表于 2004-05-15 00:43 |只看该作者

php5实现的数据分页类

前赴后继!
看来PHP朝全面面向OO进军了。呵呵,大家都在搞,形势一片大好,全国山河一片红啊。
不错。

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
5 [报告]
发表于 2004-05-15 00:43 |只看该作者

php5实现的数据分页类

前赴后继!
看来PHP朝全面面向OO进军了。呵呵,大家都在搞,形势一片大好,全国山河一片红啊。
不错。

论坛徽章:
0
6 [报告]
发表于 2004-05-15 17:27 |只看该作者

php5实现的数据分页类

什么是oo?

论坛徽章:
0
7 [报告]
发表于 2004-05-15 18:01 |只看该作者

php5实现的数据分页类

OO是面向对像,OOP表示面向对像编程
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP