您好,欢迎访问一九零五行业门户网

php pdo自动分页类代码与例子

autopage){ // 检查pagesize参数 if ($this->pagesize // 计算总记录数 $rs = @parent::query($this->rebuildsqlstring($sqlstring)); $this->rows = $rs->fetchcolumn();// 计算总页数 if ($this->rows 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 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 = new pdostatement(); $rs = @parent::query($sqlstring); $this->recordset = $rs->fetchall();//returns an array. $this->recordcount = count($this->recordset); if(!$this->autopage){$this->pages = (!$this->recordcount)?0:1;} return $this->recordcount; }// 取得字段值 public function fieldvalue($fieldname=){ return ($this->recordset[$this->mcurrentrow][$fieldname]); }//--------移动记录集游标--------------- public function move($rowpos){ if ($rowposif ($rowpos > $this->recordcount-1) $rowpos = $this->recordcount-1; $this->mcurrentrow = $rowpos; $this->eof = false; $this->bof = false; } public function movenext(){ if($this->mcurrentrow 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用户名、密码、数据库名等信息。
include_once(./pdopage_class.php); $db = new pdopage(mysql:host=localhost;dbname=mydb,root,123456); $db->execute(set character set gbk;); $db->autopage = false; $db->pagesize = 6; $db->currentpage = 1; $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(); ?>
复制代码
其它类似信息

推荐信息