PHP 分页类

<?php
/**
 * 文件名: page.class.php
 * 说明:  分页类
 * 作者:  吕耀祖
 * 联系方式:email:lvyaozu@163.com
 *    msn:lvyaozu@hotmail.com
 * 创建日期:2007-06-25
 */
class page 
{
 var 
$perpage10;  
//每页显示多少条记录数
 
var $total;    
//总记录数
 
var $curpage 1;  
//当前页码
 
var $pages;    
//总页数 
 
var $offset;   
//记录偏移量
 
var $filename;   
//分页文件名称
 
var $pagename 'page'
//分页时用来传递参数的变量 如:"$filename?page=$curpage"
 
 
function page($total,$perpage,$filename,$pagename
) {
  
$this->total intval($total
);
  if(isset(
$perpage)) $this->perpage $perpage
;
  
$this->pages ceil($this->total $this->perpage
);
  
$this->getcurpage
();
  
$this->offset = ($this->curpage-1)*$this->perpage
;
  if(isset(
$pagename)) $this->pagename $pagename
;
  if(isset(
$filename
)) {
   
$this->filename $filename
;
  } else {
   
$this->filename $_SERVER['PHP_SELF'
];
  } 
 }
 function 
getcurpage
() {
  if(isset(
$_GET[$this->pagename])) $this->curpage intval($_GET[$this->pagename
]);
  if(
$this->curpage 1$this->curpage 1
;
  if(
$this->curpage $this->pages$this->curpage $this->pages
;
 }
 
 function 
getfirstpage
() {
  if(
$this->curpage == 1
) {
   return 
"首页"
;
  } else {
   return 
"<a href=/"{$this->getlink(1)}/">首页</a>"
;
  }
 }
 function 
getnextpage
() {
  
$nextpage $this->curpage 1
;
  return 
"<a href=/"{$this->getlink($nextpage)}/">下一页</a>"
;
 }
 function 
getprepage
() {
  
$prepage $this->curpage 1
;
  return 
"<a href=/"{$this->getlink($prepage)}/">上一页</a>"
;
 }
  
 function 
getlastpage
() {
  if(
$this->curpage == $this->pages
) {
   return 
"尾页"
;
  } else {
   return 
"<a href=/"{$this->getlink($this->pages)}/">尾页</a>"
;
  }
 }
 
 function 
getlink($param
) {
  if(
strpos($this->filename,'?'
)) {
   return 
$this->filename."&".$this->pagename."=".$param
;
  } else {
   return 
$this->filename."?".$this->pagename."=".$param
;
  } 
 }
 
 function 
showpage
() {
  
$begin $this->curpage 4
;
        
$end $this->curpage 5
;
        if(
$this->curpage<=4
){
                
$begin 1
;
                
$end 10
;
        }
        if(
$this->curpage $this->pages-10
){
                
$begin $this->pages 10
;
                
$end $this->pages
;
        }
        if(
$begin<1
){
                
$begin 1
;
        }
        if(
$end>$this->pages
){
                
$end $this->pages
;
        }
        if(
$this->total>$this->perpage
) {
         
$pagenav $this->getfirstpage
();
   if(
$this->curpage 1
) {
    
$pagenav .=  $this->getprepage
();
   }
   for(
$i $begin$i <= $end$i
++) { 
    if(
$i == $this->curpage
) {
     
$pagenav .=  "$i"." "
;
    } else {
     
$pagenav .= "<a href=/"{$this->getlink($i)}/">[$i]</a>"." "

    }
   }  
   if(
$this->curpage $this->pages
) {
    
$pagenav .= $this->getnextpage
();
    
$pagenav .= $this->getlastpage
();
   }  
        }
        return 
$pagenav
;
 }
}
?>


$conn=mysql_connect('localhost','root','fkedwgwy')
  or  die('ʧܣ'.mysql_error());
 
  if (mysql_select_db('luowei',$conn))
  {
   @header("Content-Type: text/html; charset=utf-8");
     mysql_query("SET NAMES 'utf8'");
  echo'ѡݿɹ'.'<p>';
 
  }
  else
  {
  echo'ݿѡʧܣ'.mysql_error().'<p>';
  }
 
  //

 
   $result=mysql_query("select * from news");
    $count=mysql_num_rows($result);

$perpage =3;
$total =$count;
$page = new page($total,$perpage,'fenyecg.php','page');
$sql = "select * from news limit $page->offset,$perpage";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
 echo '标题'.$row['News_Title'].'id号'.$row['News_id'].'时间'.$row['News_time']."<br/>";
}
echo $page->showpage();

原文地址:https://www.cnblogs.com/fengju/p/6174160.html