简单的php实现分页效果

<?php  
    header("content-type:text/html;charset=utf-8");  
    //连接mysql数据库  
    $conn=mysql_connect("localhost","root","root");  
      
    //选择数据库  
    mysql_select_db("mysql");  
  
    //设置客户端和连接字符集  
    mysql_query("set names utf8");  
      
    //limit要求参数  
    $length=15;     //第页显示记录数 
    $pagenum=@$_GET['page']?$_GET['page']:1;  
      
    //数据总行数  
    $sql="select count(*) from help_category";
$res=mysql_query($sql);
    $count=mysql_fetch_row($res);  //$count总的记录数
    $pagetot=ceil($count[0]/$length);  //ceil作用:向上取整
  
    //限制页数  
    if($pagenum>=$pagetot){  
        $pagenum=$pagetot;  
    }  
    $offset=($pagenum-1)*$length;  
      
    //从数据库获取数据  
    $sql="select * from help_category order by help_category_id limit $offset,$length";  
  
    $result=mysql_query($sql);  
    echo "<h1>用户信息表</h1>";  
    echo "<table width='700px' border='1px'>";  
    while($row=mysql_fetch_array($result)){  
        echo "<tr>";  
        echo "<td>".$row[0]."</td>";  
        echo "<td>".$row[1]."</td>"; 
        echo "<td>".$row[2]."</td>"; 
        echo "</tr>";  
    }  
    echo "</table>";  
      
    //计算上一页和下一页 

$prevpage=$pagenum-1;  

$nextpage=$pagenum+1; 

    echo "<h2><a href='fenye.php?page=1'>[首页]</a>&nbsp;&nbsp;<a href='fenye.php?page=$prevpage'>[上一页]</a>&nbsp;&nbsp;<a href='fenye.php?page=$nextpage'>[下一页]</a>&nbsp;&nbsp;<a href='fenye.php?page=$pagetot-1'>[最后一页]</a></h2>";  
  
    //释放连接资源  
    mysql_close($conn);  
?> 
发布了14 篇原创文章 · 获赞 3 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Crazy_shark/article/details/79679562