<?php
class ShowPage
{
/**
* 翻页参数
* @var string
*/
var $pVar = "p";
var $vPage = 8;//每页显示的下标,1、2、3、4、5、6、7、8
/**
* 当前页
* @var int
*/
var $tPage;
/**
* 数据库对象
* @var conn
*/
var $conn;
/**
* 数据库表名
* @var string
*/
var $table;
/**
* 每页显示的数据条数
* @var string
*/
var $limit;
/**
* SQL语句
* @var string
*/
var $sqlQuery;
/**
* 构造函数
* @param conn $conn 数据库对象,
* @param string $table 数据库表名
* @param int $limit 每页显示的数据条数
* @param string $sqlQuery SQL語句(可選)where后面跟的查询参数
* @todo 完成其他conn对象 */
function __construct($limit,$vpage,$pVar='p') {
$this->limit= $limit;//每页显示的记录数
$this->pVar = $pVar;
$this->vPage = $vpage;//当页可以切换的页数量
}
function ShowStr(){
if ($this->tPage > 0) {//总页数量,如273条记录,可以分为28页,每页10条。$this->tPage=28;$this->limit=10;$this->$vPage=8
if($this->GetCurrentPage()<>1){//$this->GetCurrentPage()
$forpage=$this->GetCurrentPage()-$this->vPage;
if($forpage>=1){
$str_start ="<a href="".$this->file.'?'.$this->pVar."=".$forpage.""><<</a> ";
}
}
$start = (ceil($this->GetCurrentPage()/$this->vPage)-1)*$this->vPage + 1;
if($this->GetCurrentPage()<>$this->tPage){
if($this->GetCurrentPage()<$this->tPage){
$forpage= ceil($this->GetCurrentPage()/$this->vPage)*$this->vPage +1;
if($forpage<$this->tPage){
$str_end .="<a href="".$this->file.'?'.$this->pVar."=".$forpage."">>></a> ";
$end = $forpage;
}else{
$end = $this->tPage+1;
}
}else{
$end = $this->tPage+1;
}
}else{
$end = $this->tPage+1;
}
for ($i = $start; $i <$end; $i++) {
if($i!=$this->GetCurrentPage()){
$str_show .="<a href="".$this->file.'?'.$this->pVar."=".$i."">".$i."</a> ";
}else{
$str_show .=$i." ";
}
}
$str_show = $str_start . $str_show . $str_end;
}
else {
$str_show = "";
}
return $str_show;
}
function UseDB($conn, $table, $sqlQuery = ""){
$this->conn= $conn;
$this->table= $table;
$this->sqlQuery= $sqlQuery;
$tmpQuery = "SELECT * FROM $this->table";
if ($sqlQuery <> "") {
$tmpQuery = $tmpQuery." WHERE ".$sqlQuery;//by joyce
}
//adoconn hack start
$count = $conn->exec($tmpQuery) or die(print_r($conn->ErrorInfo()));
$this->tPage = ceil($count / $this->limit); // caculate the total page
//$this->tPage = $count;
}
/**
* 设置翻页参数
* @param string $pvar 翻页参数
*/
function SetPageVar($pvar = 'p') {
// set type turnpage parameter (E.G. http://www.hello.com/index.php?p=1)
$this->pVar = $pvar;
}
/**
* 返回总行数
* @return int
*/
function GetTotalRow() {
return $this->tRow;
}

/**
* 返回翻页参数
* @return string
*/
function GetPageVar() {
return $this->pVar;
}

/**
* 返回当前页数
* @return int
*/
function GetCurrentPage() {
if($_GET[$this->pVar])
return $_GET[$this->pVar];
else
return 1;
}

/**
* 返回总页数
* @return int
*/
function GetTotalPage() {
return $this->tPage;
}
/**
* 返回每页显示总条数
* @return int
*/
function GetLimit() {
return $this->limit;
}
/**
* 返回数据行数
* @return int
*/
function GetRowFrom() {
if ($this->GetCurrentPage() <= 1) {
return 0;
}
else {
return ($this->GetCurrentPage() - 1) * $this->GetLimit();
}
}
/**
*设置其他参数
* @param array $data
*/
function setVar($data) {
// set the turnpage addition value need to transfer by get mode
foreach ($data as $k=>$v) {
$this->varstr.='&'.$k.'='.urlencode($v);
}
}
}
?>
本文介绍了一个PHP分页类的设计与实现细节,包括如何通过构造函数设置每页显示的记录数、翻页参数等,并提供了获取当前页数、总页数等功能的方法。此外,还介绍了如何使用数据库对象进行数据查询及分页处理。
1349

被折叠的 条评论
为什么被折叠?



