一个浅显易懂的分页类
时间:2006-01-18 来源:wleige
query($sql);
$db->next_record();
$recordcount = $db->f(0);//记录总数
$p = new page($recordcount, 20, 15);//(总记录数,每页记录数,每面页码个数)
$p->ar = array("«首页", "‹前页", "后页›", "末页»");//可以设置成图片HTML代码
$trunpage = $p->show_page();//得到分页信息
$sql = "select * from t_publiccode".$p->limit();
//echo $trunpage."
".$sql;
$db->query($sql);
//...
类似效果:
共[52600]条记录/共[2630]页 末页>>
*/
var $pagecount; //总页数
var $limit = ""; //分页时用到的limit参数
var $pagearg = "page"; //分页参数名称
var $pagesize = 20; //每页显示记录数
var $recordcount; //总记录数
var $pagenum; //当前页码
var $argstr = ""; //GET参数字符串
var $perpage = 5; //每次显示页码个数
var $ar = array("[«]", "[‹]", "[›]", "[»]");
/*
* 功能:构造函数
* 参数 $recordcount 为记录总数
* 参数 $pagesize 为每页显示记录数,默认为20
* 参数 $perpage 为每次显示页码个数,默认为5
*/
function page($recordcount, $pagesize = 20, $perpage = 5){
$this->pagesize = $pagesize;
$this->recordcount = $recordcount;
$this->perpage = $perpage;
$this->pagecount = ceil($recordcount/$pagesize);//总页数
$this->pagenum = $this->currentpage();//当前页码
$this->argstr = $this->newarg();//GET参数字符串
$this->limit = " LIMIT ".($this->pagenum-1)*$pagesize.",".$pagesize;//分页时用到的limit参数
}
/*
* 功能:取得当前页码函数
*/
function currentpage(){
if(isset($_GET[$this->pagearg])){
if($_GET[$this->pagearg] pagearg] > $this->pagecount){
$page = $this->pagecount;
}else{
$page = $_GET[$this->pagearg];
}
}else {
$page = 1;
}
return $page;
}
/*
* 功能:重新整理GET参数
*/
function newarg(){
$str = "";
$urlar = $_GET;
unset($urlar[$this->pagearg]);
if($urlar){
foreach($urlar as $key=>$val){
if($str == "") {
$str = "?$key=$val";
}else {
$str .= "&$key=$val";
}
}
$str .= "&$this->pagearg=";
}else{
$str = "?$this->pagearg=";
}
return $str;
}
/*
* 功能:返回MYSQL语句的limit部分代码
*/
function limit(){
return $this->limit;
}
/*
* 功能:分页字符输出函数
*/
function show_page(){
$trunpage = " 共[".$this->recordcount."]条记录/共[".$this->pagecount."]页 ";
$page = $this->pagenum;
//向前翻
if($page == 1){
$trunpage .= $this->ar[0]." ".$this->ar[1]." ";
}else {
$trunpage .= 'argstr.'1">'.$this->ar[0].' argstr.($page-1).'">'.$this->ar[1].' ';
}
//中间数字翻页
$numpage = 1;
while($page - $this->perpage * $numpage > 0) $numpage++;
$startpage = ($numpage - 1) * $this->perpage;
$endpage = $this->pagecount perpage ? $this->pagecount : $numpage * $this->perpage;
for($i = $startpage + 1; $i argstr.$i."\">$i " : "".$i." ";
}
//向后翻
if($page == $this->pagecount || $this->pagecount == 0){
$trunpage .= $this->ar[2]." ".$this->ar[3]." ";
}else {
$trunpage .= 'argstr.($page+1).'">'.$this->ar[2].' argstr.$this->pagecount.'">'.$this->ar[3].' ';
}
//跳转
$select = "\n";
for($i = 1; $i pagecount; $i++){
$select .= "argstr.$i."\">$i\n";
}
$select .= "";
return $trunpage.$select;
}
}
?>
$db->next_record();
$recordcount = $db->f(0);//记录总数
$p = new page($recordcount, 20, 15);//(总记录数,每页记录数,每面页码个数)
$p->ar = array("«首页", "‹前页", "后页›", "末页»");//可以设置成图片HTML代码
$trunpage = $p->show_page();//得到分页信息
$sql = "select * from t_publiccode".$p->limit();
//echo $trunpage."
".$sql;
$db->query($sql);
//...
类似效果:
共[52600]条记录/共[2630]页 末页>>
*/
var $pagecount; //总页数
var $limit = ""; //分页时用到的limit参数
var $pagearg = "page"; //分页参数名称
var $pagesize = 20; //每页显示记录数
var $recordcount; //总记录数
var $pagenum; //当前页码
var $argstr = ""; //GET参数字符串
var $perpage = 5; //每次显示页码个数
var $ar = array("[«]", "[‹]", "[›]", "[»]");
/*
* 功能:构造函数
* 参数 $recordcount 为记录总数
* 参数 $pagesize 为每页显示记录数,默认为20
* 参数 $perpage 为每次显示页码个数,默认为5
*/
function page($recordcount, $pagesize = 20, $perpage = 5){
$this->pagesize = $pagesize;
$this->recordcount = $recordcount;
$this->perpage = $perpage;
$this->pagecount = ceil($recordcount/$pagesize);//总页数
$this->pagenum = $this->currentpage();//当前页码
$this->argstr = $this->newarg();//GET参数字符串
$this->limit = " LIMIT ".($this->pagenum-1)*$pagesize.",".$pagesize;//分页时用到的limit参数
}
/*
* 功能:取得当前页码函数
*/
function currentpage(){
if(isset($_GET[$this->pagearg])){
if($_GET[$this->pagearg] pagearg] > $this->pagecount){
$page = $this->pagecount;
}else{
$page = $_GET[$this->pagearg];
}
}else {
$page = 1;
}
return $page;
}
/*
* 功能:重新整理GET参数
*/
function newarg(){
$str = "";
$urlar = $_GET;
unset($urlar[$this->pagearg]);
if($urlar){
foreach($urlar as $key=>$val){
if($str == "") {
$str = "?$key=$val";
}else {
$str .= "&$key=$val";
}
}
$str .= "&$this->pagearg=";
}else{
$str = "?$this->pagearg=";
}
return $str;
}
/*
* 功能:返回MYSQL语句的limit部分代码
*/
function limit(){
return $this->limit;
}
/*
* 功能:分页字符输出函数
*/
function show_page(){
$trunpage = " 共[".$this->recordcount."]条记录/共[".$this->pagecount."]页 ";
$page = $this->pagenum;
//向前翻
if($page == 1){
$trunpage .= $this->ar[0]." ".$this->ar[1]." ";
}else {
$trunpage .= 'argstr.'1">'.$this->ar[0].' argstr.($page-1).'">'.$this->ar[1].' ';
}
//中间数字翻页
$numpage = 1;
while($page - $this->perpage * $numpage > 0) $numpage++;
$startpage = ($numpage - 1) * $this->perpage;
$endpage = $this->pagecount perpage ? $this->pagecount : $numpage * $this->perpage;
for($i = $startpage + 1; $i argstr.$i."\">$i " : "".$i." ";
}
//向后翻
if($page == $this->pagecount || $this->pagecount == 0){
$trunpage .= $this->ar[2]." ".$this->ar[3]." ";
}else {
$trunpage .= 'argstr.($page+1).'">'.$this->ar[2].' argstr.$this->pagecount.'">'.$this->ar[3].' ';
}
//跳转
$select = "\n";
for($i = 1; $i pagecount; $i++){
$select .= "argstr.$i."\">$i\n";
}
$select .= "";
return $trunpage.$select;
}
}
?>
相关阅读 更多 +