PHP分页基础教程之简单分页原理

<?php
include("conn.php"); //数据库链接,自己解决
 
 $sql="select * from news";
 $query=mysql_query($sql);
 $all_num=mysql_num_rows($query); //总条数
 $page_num=3; //每页条数
 $page_all_num = ceil($all_num/$page_num); //总页数
 $page=empty($_GET['page'])?1:$_GET['page']; //当前页数
 $page=(int)$page; //安全强制转换
 $limit_st = ($page-1)*$page_num; //起始数
//=================================================
 $sql="select * from news limit $limit_st , $page_num";
 
 $query=mysql_query($sql);
    while($row=mysql_fetch_array($query)){
       echo $row['title']." <a href='view.php?id={$row['id']}'>浏览</a><hr>";
}
 
 $px = $page>=$page_all_num ? $page_all_num : $page+1 ;
 
 $ps = $page<=1 ? 1 : $page-1 ;
 
?>
<a href='page.php'>首页 </a> | 
<a href='page.php?page=<?php echo $ps?>'>上一页 </a> | 
<a href='page.php?page=<?php echo $px?>'>下一页 </a> | 
<a href='page.php?page=<?php echo $page_all_num?>'>尾页 </a>

 

posted @ 2013-05-10 11:18  王翔(kingfly)  阅读(274)  评论(0编辑  收藏  举报