PHP实例:PHP分页代码_[PHP教程]
/*
* 文件名:datagridclass.php
* 作者:感染源
* 时间:2007-07-25
* 描述:分页类
*/
error_reporting(0);
class datagridclass
{
private conn;
private result;
private resultArr = array();
public beginrecord = 0;
public totalrecords = 0;
public totalpages = 0;
public currentpage = 0;
public maxline = 0;
public maxlist = 0;
function __construct(pHost, pName, pPwd, pDbName, pMaxLine = 15, pMaxList = 8)
{
this->conn = mysql_connect(pHost, pName, pPwd) or die('DataBase connecting false...');
mysql_select_db(pDbName, this->conn) or die('Choice database false...');
this->maxline = pMaxLine;
this->maxlist = pMaxList;
}//function __construct();
function __set(property_name, value)
{
this->property_name = value;
}//function __set();
function __destruct()
{
mysql_free_result(this->result);
mysql_close(this->conn);
}//function __destruct();
function readdata(pSql)
{
this->result = mysql_query(pSql, this->conn) or die('Select false...');
this->totalrecords = mysql_num_rows(this->result);
if (!this->totalrecords)
{
return false;
}
else
{
this->totalpages = ceil(this->totalrecords / this->maxline);
pSql .= ' LIMIT '.this->beginrecord.','.this->maxline;
this->result = mysql_query(pSql, this->conn) or die('Select false...');
i = 0;
while (row = mysql_fetch_array(this->result))
{
this->resultArr[i] = row;
i++;
}//while
}//if
return this->resultArr;
}//function readdata();
function navigate()
{
if (this->totalrecords)
{
this->currentpage = (this->beginrecord / this->maxline) + 1;
firstpage = 0;
prevpage = this->beginrecord - this->maxline;
nextpage = this->beginrecord + this->maxline;
lastpage = (this->totalpages - 1)*this->maxline;
if (this->beginrecord == 0)
{
echo '[首页] [上一页] ';
}
else
{
echo "[首页] ";
echo "[上一页] ";
}
if (this->currentpage <= this->maxlist)
{
for (i=1; i<=this->maxlist; i++)
{
pagerecord = (i - 1)*this->maxline;
if (this->currentpage == i)
{
echo "[i] ";
}
else
{
echo "i ";
}//if
}//for
}
elseif (this->currentpage > (this->totalpages-this->maxlist))
{
for (i=(this->maxlist); i>0; i--)
{
pagerecord = (this->totalpages - i)*this->maxline;
if (this->currentpage == (this->totalpages-i+1))
{
echo "[".(this->totalpages - i +1)."] ";
}
else
{
echo "".(this->totalpages - i+1)." ";
}//if
}//for
}
else
{
j = ceil(this->maxlist / 2);
for (i = j; i>=0; i--)
{
if (this->currentpage == (this->currentpage-i))
{
echo "currentpage-i-1)*this->maxline)."'>[".(this->currentpage-i)."] ";
}
else
{
echo "currentpage-i-1)*this->maxline)."'>".(this->currentpage-i)." ";
}
}
for (i = 1; i<=j; i++)
{
echo "currentpage+i-1)*this->maxline)."'>".(this->currentpage+i)." ";
}
}
if (this->beginrecord == lastpage)
{
echo '[下一页] [末页]';
}
else
{
echo "[下一页] ";
echo "[末页]";
}
//echo "this->currentpage / this->totalpages,共有记录数:this->totalrecords";
}
}
}
/*/test
dg = new datagridclass('localhost','root','root','test',2,5);
if (isset(_GET['beginrecord']))
{
dg->__set('beginrecord', _GET['beginrecord']);
}
else
{
dg->__set('beginrecord', 0);
}
rs = dg->readdata('select * from testuser');
if(!rs)
{
echo '暂无数据……';
}
else
{
i=0;
echo "
";
echo "
id
name
";
while(i {
echo "
";
echo rs[i]['id'];
echo "
".rs[i]['uname'];
echo "
";
i++;
}
echo "";
}
dg->navigate();
dg = NULL;
*/
?>
来源:CSDN