一个功能比较全的php+mysql分页显示的例子


include "header.php";
?>
<script>
function winp(id)
{
window.open("/read.php?table=php&id="+id,"","height=550,width=750,resizable=yes,scrollbars=yes,status=no,toolbar=no,menubar=no,location=no");
        }
</script>


<?

//list article in the first page limit 5

$table=php;//表名称
$perpage=15;//每页显示文章数目
$my_link =  mysql_connect('localhost','root',''); 
$db=mysql_select_db("article",$my_link);
$query="SELECT COUNT(*) AS rcnt FROM $table"
$result=mysql_query($query$my_link); 
$data=mysql_fetch_array($result); 

$nr=$data["rcnt"];  //文章总数


//$jumpto为跳转到页面数

if (isset($jumpto))
{
  if (
$jumpto<=$pages)
     
$offset=$jumpto;
     else      
$offset=$last_offset;
 }

//判断偏移量参数是否传递给了脚本,如果没有就使用默认值0 
if (empty($offset)) 
 { 
    
$offset=0
          } 
?>

<table width="98%" cellspacing="0" cellpadding="0" bordercolordark="#FFFFFF"
bordercolorlight="#000000" height="34" align="center" border="1">
  <tr class="black" bgcolor="#FFeeCC">
    <td width="50%"><p align="center">主&nbsp;&nbsp;&nbsp;&nbsp;题 </td>
    <td width="15%"><p align="center">发布时间 </td>
    <td width="8%"><p align="center">人气 </td>
  </tr>
  
<?
$query 
"select id,title,visit,upload from php order by id desc LIMIT $offset,$perpage"
$result mysql_query($query$my_link); 
         
while(
$row mysql_fetch_array($result)) {
 
$id=$row["id"];
 echo 
"<tr width="100%"><td width="50%"><div><dd>";
     echo 
"<a href=javascript:winp($id)>";
     echo 
$row["title"]."</a>";
 echo 
"</dd> </div></td><td width="15%" class="black"><p align="center">&nbsp";
     echo 
$row["upload"];
     echo 
"</td><td width="8%" class="black"><p align="center">";
     echo 
"<font FACE="COURIER"color=red>&nbsp".$row["visit"]."</font></font><br>";
 echo 
"</td></tr>";
}
 
?>
</table>



<form method=post action=<?=$PHP_SELF;?>
<table bordercolor=#ffffff cellspacing=0 cellpadding=0 width="620" align=center border=1 bordercolorlight="#000000">
<tr><td width=770>
<a href="<?=$PHP_SELF;?>?offset=0">首页</a> 
<?

if($offset//如果偏移量是0,不显示前一页的链接 

$preoffset=$offset-$perpage
print 
"<a href="$PHP_SELF?offset=$preoffset">前一页</a>&nbsp;n"

//计算总共需要的页数 
$pages=ceil($nr/$perpage); //$pages变量现在包含所需的页数 


//检查是否是最后一页 
//if ($pages!=0 && ($newoffset/$perpage)!=$pages) 
$nextoffset=$offset+$perpage;
if ((
$pages!=0) && ($nextoffset<$nr))   

  print 
"<a href="$PHP_SELF?offset=$nextoffset">下一页</a>&nbsp;n"
  


  
$last_offset=($pages-1)*$perpage;
  
$this_page_no=ceil($offset/$perpage)+1;
?> 

 <a href="<?=$PHP_SELF;?>?offset=<?=$last_offset;?>">尾页</a>
 页次:<font class="red"><?=$this_page_no;?>/</font><?=$pages;?>
 共<font class="red"><?=$nr;?></font>篇文章 
 <?=$perpage;?>篇/页 
 转到第:</span>

<input type="text" name="jumpto" size="2" maxlength="4">页
<input type="submit" name="Submit2" value="go" class="buttonface"><br>


<?
for ($i=0$i $pages$i++) 

  
$newoffset=$perpage*$i
  print 
"<a href="$PHP_SELF?offset=$newoffset">";
  echo 
abs($i+1);
  echo 
"</a>&nbsp;n"

?>
</td></tr></table>
</form>

<?include "footer.php";?>