一个继承自mysqli类的自动分页类

1、新建一个PHP文件,文件名为mysqlipage_class.php,将以下代码粘贴进去。



代码如下:



<?php

/**

 * 类名: MysqliPage

 * 作者:谢声涛 shishengsoft@gmail.com

 * 描述: 继承自mysqli类,增加了自动分页功能,类似MS ADO组件的自动分页功能。

 */

//-------------开始-----------------------------------

class MysqliPage extends mysqli { 

 public $RecordCount = 0;    // 记录集的记录总数

 public $AutoPage = false;   // 启用自动分页功能

 public $PageSize = 0;       // 每页的记录行数

 public $CurrentPage = 0;    // 当前页

 public $Pages = 0;          // 总页数

 public $BOF = false;        // 游标到记录集之前

 public $EOF = false;        // 游标到记录集之后

 

 private $RecordSet = null;  // 记录集 

 private $mCurrentRow = -1;  // 记录集中当前游标位置

 private $Rows = 0;          //总记录数

 

 // 关闭连接

 public function Close(){unset($this);} 

 // 分页查询

 public function QueryEx($SqlString){

  // 是否启用自动分页功能

  if($this->AutoPage){

   // 检查PageSize参数

   if ($this->PageSize <=0) die("警告:PageSize不能为负数或零。");

   // 计算总记录数

   $rs = parent::query($this->rebuildSqlString($SqlString));  

   $row = $rs->fetch_array(MYSQLI_BOTH);

   $this->Rows = $row[0];   

   // 计算总页数

   if ($this->Rows < $this->PageSize) {$this->Pages = 1;}

   elseif ($this->Rows % $this->PageSize) {$this->Pages = intval($this->Rows/$this->PageSize)+1;}

   else {$this->Pages = intval($this->Rows/$this->PageSize);}

   // 约束CurrentPage值,使之位于1到Pages之间。

   if($this->CurrentPage < 1) {$this->CurrentPage =1;}

   if($this->CurrentPage > $this->Pages) {$this->CurrentPage = $this->Pages;}  

   //计算偏移量

   $Offset = $this->PageSize * ($this->CurrentPage - 1);

   // 重组SQL语句,SqlString有分号则去掉

   $SqlString = str_replace(";","",$SqlString) . " LIMIT $Offset,$this->PageSize;";   

  }  

  // 查询并返回记录集

  $rs = @parent::query($SqlString);//returns a result object.

  $this->RecordSet = $rs;

  $this->RecordCount = $rs->num_rows; 

  if(!$this->AutoPage){$this->Pages = (!$this->RecordCount)?0:1;}

  return $this->RecordCount;

 } 

 // 取得字段值

 public function FieldValue($FieldName=""){

  $this->RecordSet->data_seek($this->mCurrentRow);

  $row = $this->RecordSet->fetch_array(MYSQLI_BOTH);

  return ($row[$FieldName]);

 } 

 //--------移动记录集游标---------------------------------------------

 public function Move($RowPos){

  if ($RowPos<0) $RowPos = 0;

  if ($RowPos > $this->RecordCount-1) $RowPos = $this->RecordCount-1;

  $this->mCurrentRow = $RowPos;

  $this->EOF = false;

  $this->BOF = false;  

 }

 public function MoveNext(){

  if($this->mCurrentRow < $this->RecordCount-1){

   $this->mCurrentRow++;   

   $this->EOF = false;

   $this->BOF = false;

  }

  else{

   $this->EOF = true;

  }

 }

 public function MovePrev(){

  if($this->mCurrentRow > 0){

   $this->mCurrentRow--;

   $this->EOF = false;

   $this->BOF = false;

  }else{

   $this->BOF = true;

  } 

 }

 public function MoveFirst(){

  $this->mCurrentRow = 0;

  $this->EOF = false;

  $this->BOF = false;

 } 

 public function MoveLast(){

  $this->mCurrentRow = $this->RecordCount-1;

  $this->EOF = false;

  $this->BOF = false;

 }

 //--------------------------------------------------

 // 用于执行插入、修改、删除等操作

 public function Execute($SqlString){

  return @parent::query($SqlString);

 }

 //-----------------私有函数-----------------------------

 // 重新构造SQL语句,如将"select * from tb2"改写为"select count(*) from tb2",旨在提高查询效率。

 private function rebuildSqlString($SqlString){

  if(preg_match("/select[ ,./w+/*]+ from/",$SqlString,$marr)){  

   $columns = preg_replace("/select|from/","",$marr[0]); 

   $columns = preg_replace("//*/","/*",$columns);

   $result = preg_replace("/$columns/"," count(*) ",$SqlString); 

   return $result;

  }

 } 

 //-------------结束-----------------------------------

}

//-------------结束-----------------------------------



?> 



 



2、使用示例:



请根据自己的情况修改MySQL用户名、密码、数据库名等信息。



<?php

include_once("./mysqlipage_class.php");



$db = new MysqliPage("localhost","root","123456","mydb");

$db->Execute("set character set gbk;");



$db->AutoPage = true;

$db->PageSize = 6;

$db->CurrentPage =2;

$db->QueryEx("select * from tb2;");



$db->MoveFirst();

while (!$db->EOF) {

 echo  $db->FieldValue("id"),"/t",$db->FieldValue("name"),"/t",$db->FieldValue("age"),"/n";

 $db->MoveNext();

}



$db->Close();



?>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值